0

If I have an animation which can play with an array of images.

How Do I set this to play on a button? I want the buttons image to display the animation?

Thanks

some_id
  • 29,466
  • 62
  • 182
  • 304

3 Answers3

1

If you need an actual button, then the choice is clear: You will need to build your own custom UIControl object to achieve this. Quite possibly you can just subclass UIButton, and handle animating the images that way. I do something similar in one of my apps using core animation to fade between a series of 6 images. It works quite well.

I will point you at the UIControl class reference. Please read the intro material on the page, it links to other pages, like event handling. As well, I will also point you at the CABasicAnimation class reference, with the same caveat applied.

Remember, a UIControl is just a UIView, except that it handles events a particular way.

jer
  • 20,094
  • 5
  • 45
  • 69
  • This sounds great, but could you point me in a direction or go a little deeper into the topic please? :) – some_id Dec 12 '10 at 15:37
  • Another thing would be, if the button is pressed during animation, and I assign a new image to the button using the IBAction, will it kill the animation or wait for it to finish? – some_id Dec 12 '10 at 15:38
  • Edited the answer above, hope that helps. With respect to your question about tapping the button during the animation, the tap is very likely not to be received by the button until after the animation is complete. This has been my experience in the past. – jer Dec 12 '10 at 17:26
0

Simplest way is just to place a custom UIButton (ie invisible to the user) above your UIImageView. Then it will appear that the button is animated but in reality it will be the images underneath the button that are animated.

h4xxr
  • 11,385
  • 1
  • 39
  • 36
  • I have some more complex things going on which I need the actual buttons to be displaying their own content. But I use this technique for some other apps. :P – some_id Dec 12 '10 at 14:09
0

Create an array, eg. myArray with the filenames of your images. assign this array to a UIImage with myImage.animationImages = myArray; then use setImage:forState to assign this animation to the button.

lavelle
  • 1,446
  • 1
  • 17
  • 30
  • "then use setImage:forState to assign this animation to the button." ? I dont see where the link is. setImage: someButton ? Also, do I need a UIImage extra for each button I have? – some_id Dec 12 '10 at 14:11