Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating UIButton background image

Is there a way to animate a UIButton background image view with a set of images?

I've managed to do so with the imageView property, but couldn't find an equivalent background property.

10x

like image 383
Rizon Avatar asked Jan 15 '12 20:01

Rizon


1 Answers

Here's what I did (inside a UIButton subclass):

Set the button images, like regular:

[self setBackgroundImage:[[UIImage imageNamed:@"button"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)] forState:UIControlStateNormal];
[self setBackgroundImage:[[UIImage imageNamed:@"button_pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)] forState:UIControlStateHighlighted];

Override highlighted:

- (void)setHighlighted:(BOOL)highlighted {
     [UIView transitionWithView:self duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:^{
         [super setHighlighted:highlighted];
     } completion:nil]; 
}
like image 121
kgaidis Avatar answered Oct 16 '22 05:10

kgaidis