Basic iOS animation


SDK Version: 
M3

Sometimes you may need to display short animations in your applications like funny GIFs. The problem with GIFs is that they have only 256 colors and poor transparency. You can consider using PNGs, but basically that type of image doesn't support animations. So what can you do to push the advantage of PNGs (great lossless compression, alpha channels, various coloring schemes, etc.)?

In your iOS app you can simply define the animation frame images of PNGs and tell the app to use these frames in an animation. Just that simple. I'll now show you how to do that.

I will use JPGs for this time but the same works for PNGs as well. You need to drag the frame images into your project, like this:

project

After this you will need an UIImageView and an NSArray for the frames:

header

I am using an IBOutlet which is linked to an UIImageView in the Interface Builder.
For testing purposes, my implementation look as follows:

m

When the view is loaded, it populates the frame array with the images in order. Here you can set such properties like the animation duration which is for the whole animation with all the images in the array. If you would like to set the repeat count to infinite, you should set the corresponding property to 0. In this example the animation starts immediately when the view appears and runs only once.

I've made an animation like this with the code above:

airplane

For the android version of the code above you can find examples at http://developer.android.com/guide/topics/resources/animation-resource.html#Frame.

Feel free to rewrite the code and use it as needed.