views:

415

answers:

1

I'd like to give a UIButton in my app a continuous "pulsing" effect where it animates through a series of images and then reverses back through these same images. I've found examples of UIImageViews using their animationImages property, but all I've found for UIButtons is animating changing their alpha.

The UIView method setAnimationRepeatAutoreverses is what I want for the looping, but I don't understand how to use an array of images within an animation block for a UIButton. Is this possible or is a NSTimer/setImage approach recommended? Any examples are much appreciated.

+1  A: 

You could add an UIImageView as a subview of your button and use its animationImages property. If you want to use your UIButton's backgroundImage property, you could try to find its corresponding UIImageView as a child of UIButton, and apply the same animationImages technique.

luvieere
Hmm I deleted the UIButton's default image in IB and tried to add the UIImageView via the UIButton's outlet in viewDidLoad: UIImageView *myButtonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myButton_ON_01.png"]]; [self.myButton addSubview:myButtonImage]; [self.myButton bringSubviewToFront:myButtonImage]; [self.myButton setNeedsLayout];I also tried [self.myButton setNeedsDisplay]; instead of setNeedsLayout but I can't get the image to show up.
Ian
Well I'll be... I just came back to this, starting tearing it out and now it works! I just had to set the animationImages, animationDuration, and then startAnimating. Thank you!
Ian