Ah, the animated gif, a throwback to the early days of web development and simple animation. In this post I’ll show you how to use a UIImageView to create a similar effect that you get with an animated gif.
Below I’ve inserted the animated gif that I’ll use as a template for the iPhone app we’ll write in this post.
Animated gifs are nothing more than a series of images, encapsulated within one gif file, that are displayed in sequence. Within an animated gif one can specify the duration that each image (aka frame) is displayed, which offers a wide range effects that you can achieve.
Animated Images on the iPhone
To achieve a similar effect in an iPhone app, we create a UIImageView and set the animationImages property to point to an array of images. Not unlike the animated gif, we now have a series of images in which we can control the amount of time to complete one cycle of the images, as well as the repeat count of how many times to cycle through the animation.
To keep the example short, I’ll use only one class, the application delegate. Here is how I’ve defined the delegate class:
@interface animationAppDelegate : NSObject <UIApplicationDelegate>
Not much here, the UIImageView, an array to hold the images and the UIWindow for the output.
The code for the implementation of the app delegate follows:
#define IMAGE_COUNT 36
#define IMAGE_WIDTH 240
#define IMAGE_HEIGHT 180
#define STATUS_BAR_HEIGHT 20
#define SCREEN_HEIGHT 460
#define SCREEN_WIDTH 320
- (void)applicationDidFinishLaunching:(UIApplication *)application
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Array to hold jpg images
imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];
// Build array of images, cycling through image names
for (int i = 0; i < IMAGE_COUNT; i++)
[imageArray addObject:[UIImage imageNamed:
[NSString stringWithFormat:@"Frame_%d.jpg", i]]];
// Animated images - centered on screen
animatedImages = [[UIImageView alloc]
(SCREEN_WIDTH / 2) - (IMAGE_WIDTH / 2),
(SCREEN_HEIGHT / 2) - (IMAGE_HEIGHT / 2) + STATUS_BAR_HEIGHT,
animatedImages.animationImages = [NSArray arrayWithArray:imageArray];
// One cycle through all the images takes 1 seconds
animatedImages.animationDuration = 1.0;
// Repeat forever
animatedImages.animationRepeatCount = 0;
// Add subview and make window visible
// Start it up
// Wait 5 seconds, then stop animation
[self performSelector:@selector(stopAnimation) withObject:nil afterDelay:5];
How it Works
On lines 3 – 5 we define the attributes for the images in the animation. There are a total of 36 images, each is 240 x 180 pixels. On line 22 the array is defined which will hold the images and lines 25 – 27 the images are added to the array. You can decipher from the code that the images I am using have file names in the format: Frame_1.jpg, Frame_2.jpg, Frame_3.jpg, etc.
On lines 30 – 34 we allocate an UIImageView object and configure the frame so the image is centered on the device display. Line 35 sets the animationImages property to the array of images created above.
Line 38 sets the duration for one cycle through all the images. We specify the images are to cycle indefinitely by setting the animationRepeatCount to -1 on line 41.
At this point we are pretty much good to go, all we need to do is add the UIImageView object as a subview (line 44) and start the animation (line 48).
Application in the iPhone Simulator
The movie below shows how the application looks when running in the iPhone simulator:
Porting Animated Gif to iPhone
Not unlike I did for the example above, if you have an animated gif that you would like to use within an iPhone app, it’s as simple as extracting/saving each image inside a gif and using a sequential naming convention. I opened the original gif file and manually saved each frame in the image to a jpg file. There are a number of tools that will do the extraction/saving/renaming for you (search for gif extractor).
With all the images saved as separate file, swap the image attributes in the #define section and update the code on line 27 to match the filename you used for saving each file.
Xcode Project Source Code
Download the Animation Xcode project.
|Animated gif of Newtons cradle, courtesy: Wikipedia|