4

I create a custom button, because I want a gradient button. so I use this code to implement it.

 @implementation CustomButton


- (id)initWithFrame:(CGRect)frame
{
    if((self = [super initWithFrame:frame])){
        [self setupView];
    }

    //[self addObserver:self forKeyPath:@"highlighted"  options:0 context:nil];
    return self;
}

- (void)awakeFromNib {
    [self setupView];
}

# pragma mark - main

- (void)setupView
{
    self.layer.cornerRadius = 10;
    self.layer.borderWidth = 1.0;
    self.layer.borderColor = [UIColor colorWithRed:167.0/255.0 green:140.0/255.0 blue:98.0/255.0 alpha:0.25].CGColor;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowRadius = 1;
    [self clearHighlightView];

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.layer.bounds;
    gradient.cornerRadius = 10;
    gradient.colors = [NSArray arrayWithObjects:
                       (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                       (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                       (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                       (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                       (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,                         nil];
   // float height = gradient.frame.size.height;
    gradient.locations = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0f],
                          [NSNumber numberWithFloat:0.5f],
                          [NSNumber numberWithFloat:0.5f],
                          [NSNumber numberWithFloat:0.8f],
                          [NSNumber numberWithFloat:1.0f],
                          nil];
    [gradient setBackgroundColor:[UIColor redColor].CGColor];
    [self.layer insertSublayer:gradient atIndex:0];
}

Now the gradient has been completed. But when I press this button , there is no highlighted status on it.

I just want when pressed its color has been deepen. Does anyone know how to implement this? Thanks

zedzhao
  • 517
  • 1
  • 3
  • 17
  • 1
    can you add the implementation of your custom button as well please – Ushan87 May 14 '13 at 12:02
  • I add the subclass code here. I just want when pressed the button's color can be deepen. Do I have to change the color myself. Like if the button is red, when pressed, it should be deepen. if so I have to know the components of the deepen color. Eh, I don't know how to get the RGB color. Thanks – zedzhao May 14 '13 at 12:16

2 Answers2

1

If you subclass UIButton you can override the setHighlighted method. Here you can set a different Gradient on the button.

- (void)setHighlighted:(BOOL)highlighted
 {
    [super setHighlighted:highlighted];

}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Using Gradients For UIButton is a lot of headache, and heavy on the memory as well, specially if u want a highlighted state also.

have a look at this Tutorial . which will explain the use of images and Gradients, for UIButton.

Using Images is fairly easy and always suggested,

Bonnie
  • 4,943
  • 30
  • 34