How to do Animation With Cocoa Touch

Matthew Campbell, September 1st

Here is a real easy way to do simple animation in Cocoa-Touch.

When you are doing iPhone programming you have a few ways to do graphics and simple animation. If you are doing extensive custom graphics you must use Core Graphics and Core Animation.

However, if you simply want to animate a simple series of images similar to an animated gif you can simple use UIImageView. What you need to do is load your series of images into the image view and then send a message to your image view to start animating. It is very simple.

Here is how you would do this in code.

What the above code will do is flash through each image in the array. It will transition smoothly so if you wanted to you could feel it a series of images like a cartoon strip or a flickering flame and it will appear to move like a film.

Ok, so as was just noted by @alexcschaefer (on Twitter) if you follow the above example you will get a memory leak. I had put everything in essentially one method for clarity. But, generally you will do something like this in the context of a view controller. You would probably declare the imageView to stay in scope throughout the view controller’s lifetime and release it in your dealloc method like so:

Or you could simply declare it in viewDidLoad and then release after adding it the the subview collection. As always, use your best judgement.

How to do Animation With Cocoa Touch…

You’ve been kicked (a good thing) – Trackback from – iPhone SDK links, community driven…

  • Hours of videos, demos & screen casts
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.