Animating UIButton images
I'd like to give a UIButton in my app a continuous "pulsing" effect where it animates throug开发者_StackOverflowh 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.
While the subview technique works, a simpler way is to just set the animationImages property on the UIButton's imageView. From the docs:
Although [the imageView] property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view.
For example:
self.pulsingButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:@"button1.png"],
[UIImage imageNamed:@"button2.png"],
nil];
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.
精彩评论