UIButton Background Image Animation

Is there a way to animate a representation of a UIButton background image using a set of images?

I managed to do this using the imageView property, but could not find an equivalent background property.

10x

+7
source share
4 answers

To change the background image of buttons or text in this regard - you need to change it for a specific UIControlState ... ie highlighted, selected, disabled

Using

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

I'm not sure that you can revive them, I have never tried. If you cannot animate them, you can put the UIImageView behind a transparent button and instead animate the images, not just thoughts.

+1
source

Here is what I did (inside a subclass of UIButton):

Set the button images as usual:

 [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 selection:

 - (void)setHighlighted:(BOOL)highlighted { [UIView transitionWithView:self duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:^{ [super setHighlighted:highlighted]; } completion:nil]; } 
+11
source

You can animate the UIButton way of animating a UIImageView like this ...

 -(void) addAnimationToButton:(UIButton*) button{ UIImageView* animationView = [button imageView]; NSArray* animations=[NSArray arrayWithObjects: [UIImage imageNamed:@"sprite1.png"], [UIImage imageNamed:@"sprite2.png"], [UIImage imageNamed:@"sprite2.png"], //and so on if you need more nil]; CGFloat animationDuration = 2.0; [animationView setAnimationImages:animations]; [animationView setAnimationDuration:animationDuration]; [animationView setAnimationRepeatCount:0]; //0 is infinite } 

And you use it as follows:

 [self addAnimationToButton:yourButton]; [[yourButton imageView] startAnimating]; //or stopAnimating 
+1
source

Yes it is possible. Use NSTimer,

NSTimer * myTimer = [NSTimer scheduleTimerWithTimeInterval: 5.00 target: self selector: @selector (changeImage) userInfo: nil repeatts: YES];

then the method is as follows:

 - (void)changeImage { static int counter = 0; if([bannerArray count] == counter) { counter = 0; } [button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerArray[counter]]]] forState:UIControlStateNormal]; counter++; } 

It worked for me. But adding animations like flip, etc., needs to be figured out. I hope this is one way to help you.

0
source

All Articles