How to Animate a Sprite in Objective-C / iOS / Cocoa Touch


A friend of mine recently asked me how to animate a sprite in iOS. He’s a very accomplished artist ( really a modern day renaissance man ), and is familiar with animation in Photoshop and Flash, but kinda new to iOS programming. Here’s his question:

…so I know how to make a gif animation in Photoshop for the web – but how does this differ when prepping my animation frames for iOS?

(W)hat do I need to know to animate my character frames for iOS.

(w)hat software actually runs the animation in iOS – is it something in Obj.C’s native code or is it simply flash or other?

And here’s my response:

To animate in iOS/Cocoa Touch you use the Objective-C UIImageView object to create animations. The UIImageView object has several methods to facilitate animating with ease. Here are the steps for animating a sprite:

1. Set up an array with all of your images that you want to animate
2. Create a UIImageView and set the size
3. Assign the array created in step one to the property animationImages of the UIImageView that you created in step two.
4. Set the animationDuration property of the UIImageView object.
5. Add the UIImageView to the view that you’re using
6. Start the animation with the UIImageView’s startAnimating method
7. If you need to stop the animation you can use UIImageView’s stopAnimating method.

It seems like a lot of steps, but it’s really quite simple. The has an excellent tutorial on how to do this:

iPhone Programming Tutorial: Animating A Game Sprite

For those who want to know how to do this using Cocos2D here’s an excellent video on how to do it: