iOS Programming 101: Simple Animation Using UIImageView

As you may know, we have released our first iOS game on App Store. Honestly, we do not have any game development experience. As we always said, “getting your hands dirty is the best way to learn programming”.

We started out by developing a simple memory game. We’re sure that it won’t be a big hit. However, it’s a great learning experience even the game do not generate good money. In coming tutorials, we’ll share with you some of the implementation techniques such as game center integration, in-app purchase implementation, Nextpeer integration for multiplayer support, etc. To start off, let’s see how you can use UIImageView to implement simple animation.

simple animation

UIImageView for Animations

In iOS, creating sophisticated animations does not require you to write complex code. The UIImageView class provides the easiest way for developers to implement animations. All you need to do is to create an UIImageView object with a series of images for animating. Once you trigger the animation, the image view will automatically render the individual frames. Cool, right?

Cartoon Images

Image series for animation

The UIImageView makes it very easy to implement simple animation. Let’s create a new Xcode project and see how it works.

Creating SimpleAnimation Project

First, fire up Xcode (make sure you’re using 4.6 or up) and create a new project using “Single View application” template.

Xcode Simple Animation Project

Create a new project using the Single View Application template

Click “Next” to continue. In reference to the below figure, fill in all the required values for the Xcode project. Make sure you enables the “Use Storyboards” option.

Xcode Simple Animation Project Settings

Simple Animation Project Settings

Click “Next” to continue. Xcode then asks you where you saves the “SimpleAnimation” project. Pick any folder (e.g. Desktop) to save your project.

Implementing the ViewController

Before implementing the ViewController class, first download the image pack which includes all images for creating the animation. Unzip the image pack and add all images to the SimpleAnimation project.

In the project navigator, select the “ViewController.m” and edit the “viewDidLoad” method with the following code:

That’s the code we need to create the animation. We first load all the images and save it into an array. We then create an UIImageView object and provide it with the image array as the value of animationImages. This array represents the “frames” of a dancing cartoon. You can control the frame rate by setting the animationDuration property. The property specifies the amount of time it takes to go through one cycle of the images. In the example, it takes 0.5 second to go through 16 images.

Optionally, you can also set the animationRepeatCount property, that specifies the number of times to repeat the animation. If it’s not set, the animation will be repeated indefinitely.

With all the properties configured, you can simply call up startAnimating method to begin animating the image. Compile and run the app to see how it works.

In order to give you a better idea about the animationDuration property, try to add a slow motion animation by adding the below code in the “viewDidLoad” method:

The above code will add another animation but with different animation duration. Compile and run the app again. Your app should look like this:

SimpleAnimation Deliverable

SimpleAnimation App

What’s Coming Next?

UIImageView provides a very easy way to implement interesting animation. Later, we’ll look into other ways to create visual effects. For your complete reference, you can download the whole Xcode project here.

As always, feel free to leave us comment and feedback. And don’t just read the tutorial. Remember to “get your hands dirty”. This is the best way to learn programming.

iOS Programming 101: Customize UITableView and UITableViewCell Background using Storyboard
How to Beta Test Your App Using TestFlight
Getting Started with Fastlane for Automating Beta Testing and App Deployment