Wednesday, June 17, 2009

Semi-transparent overlay for UIButton subclass

A little snippet for subclassed UIButton that adds an UIImageView overlay.
You can then animate the alpha property.
- (void)awakeFromNib
UIImage *img = [self imageForState:UIControlStateSelected];
overlayImage = [[UIImageView alloc] initWithImage:img];
overlayImage.alpha = 0.0f;
[self addSubview:overlayImage];
// [self addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
// you could call custom highlight handler

- (void)setHighlightedByAlpha:(BOOL)highlighted
[UIView beginAnimations:nil context:@"hmm"];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.2f];
overlayImage.alpha = highlighted ? 1.0f : 0.0f;
[UIView commitAnimations];

1 comment:

Anonymous said...

demo any?