iPhone Application Development : Creating and Managing Image Animations and Sliders (part 1)

1/30/2011 9:49:20 AM
There’s something about interface components that move that make users take notice. They’re visually interesting, attract and keep attention, and, on the iPhone’s touch screen, are fun to play with. We take advantage of both of our new UI elements (and some old friends) to create a user-controlled animation.

Implementation Overview

As mentioned earlier, image views can be used to display image file resources and show simple animations, whereas sliders provide a visual way to choose a value from a range. We’ll combine these in an application we’re calling ImageHop.

In ImageHop, we’ll be creating a looping animation using a series of images and an image view instance (UIImageView). We’ll allow the user to set the speed of the animation using a slider (UISlider). What will we be using as an animation? A hopping bunny. What will the user control? Hops per second, of course! The “hops” value set by the slider will be displayed in a label (UILabel). The user will also be able to stop or start the animation using a button (UIButton).

Figure 1 shows the completed application in use.

Figure 1. ImageHop uses an image view and a slider to create and control a simple animation.

We should discuss two pieces of this project before getting too far into the implementation:

  • First, image view animations are created using a series of images. I’ve provided a 20-frame animation with this project, but you’re welcome to use your own images if you prefer.

  • Second, although sliders enable users to visually enter a value from a range, there isn’t much control over how that is accomplished. For example, the minimum value must be smaller than the maximum, and you can’t control which dragging direction of the slider increases or decreases the result value. These limitations aren’t show-stoppers; they just mean that there may be a bit of math (or experimentation) involved to get the behavior you want.

Setting Up the Project

Begin this project in the same way as the last. Launch Xcode (Developer/Applications), and then choose File, New Project.

Select the iPhone OS Application project type, and then find and select the View-Based Application option in the Template list on the right. Click Choose to continue, enter the project name ImageHop, and save the new project.

Adding the Animation Resources

This project makes use of 20 frames of animation stored as PNG files. The frames are included in the Images folder within the ImageHop project folder.

Because we know up front that we’ll need these images, drag them into the Xcode project’s Resources folder, being sure to choose the option to copy the resources if needed.

Preparing the Outlets and Actions

In this application, we need to provide outlets and actions for several objects.

For outlets, first we need the image view (UIImageView), which will contain the animation and be referenced through the variable imageView. The slider control (UISlider) will set the speed and will be connected via animationSpeed, while the speed value itself will be output in a label named hopsPerSecond (UILabel). A button (UIButton) will toggle the animation on and off and will be connected to an outlet toggleButton.

By the Way

Why do we need an outlet for the button? Shouldn’t it just be triggering an action to toggle the animation? Yes, the button could be implemented without an outlet, but by including an outlet for it, we have a convenient way of setting the button’s title in the code. We can use this to change the button to read “Stop” when the image is animating or “Start” when the animation has stopped.

For actions, we need only two: setSpeed will be the method called when the slider value has changed and the animation speed needs to be reset, and toggleAnimation will be used to start and stop the animation sequence.

Go ahead and define these outlets and actions as outlets and actions within ImageHopViewController.h. You’ll also want to declare the four outlet variables as properties so that we can easily access them in the view controller code. Listing 1 shows the resulting header file.

Listing 1.
 1: #import <UIKit/UIKit.h>
3: @interface ImageHopViewController : UIViewController {
4: IBOutlet UIImageView *imageView;
5: IBOutlet UIButton *toggleButton;
6: IBOutlet UISlider *animationSpeed;
7: IBOutlet UILabel *hopsPerSecond;
8: }
10: @property (retain,nonatomic) UIImageView *imageView;
11: @property (retain,nonatomic) UIButton *toggleButton;
12: @property (retain,nonatomic) UISlider *animationSpeed;
13: @property (retain,nonatomic) UILabel *hopsPerSecond;
15: -(IBAction)toggleAnimation:(id)sender;
16: -(IBAction)setSpeed:(id)sender;
18: @end

For all the properties you’ve defined in the header file, add an @synthesize directive in the ImageHopViewController.m implementation file. Your additions should fall after the @implementation line and look like this:

@synthesize toggleButton;
@synthesize imageView;
@synthesize animationSpeed;
@synthesize hopsPerSecond;

Make sure that both the ImageHopViewController header and implementation files have been saved, and then launch Interface Builder by double-clicking the ImageHopViewController.xib file within the project’s Resources folder.

After it has loaded, switch to the Document window (Window, Document), and double-click the view icon to open it and begin editing.

  •  iPhone Application Development : User Input and Output
  •  Windows Phone 7 : Using Accelerometer Data to Move a Ball
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 4) - Device Libraries
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 2) - Detecting the Context
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 1) - HTTP
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 2)
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 1)
  •  Using Windows Phone 7 Technologies : Understanding Orientation and Movement
  •  Programming the Mobile Web : HTML 5 (part 4) - Client Storage
  •  Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
  •  Programming the Mobile Web : HTML 5 (part 2) - The canvas Element
  •  Programming the Mobile Web : HTML 5 (part 1)
  •  Windows Phone 7 : Submitting Your First Windows Phone Application to the Windows Phone Marketplace
  •  Windows Phone 7 : Packaging, Publishing, and Managing Applications
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 3)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 2)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 1)
  •  Programming the Mobile Web : Mobile Rich Internet Applications (part 2) - JavaScript Mobile UI Patterns
  •  Programming the Mobile Web : Mobile Rich Internet Applications (part 1) - JavaScript UI Libraries
    Top 10
    New Touch Controls Show Up
    Bigger Mini Meets Smaller Big Rivals (Part 2) : Amazon Kindle Fire HD, Google Nexus 7, Versus Touchpad 7
    Bigger Mini Meets Smaller Big Rivals (Part 1) : iPad Mini VS 7In Rivals, Samsung Galaxy Tab 2 7.0
    Phabulous Devices (Part 3)
    Phabulous Devices (Part 2)
    Phabulous Devices (Part 1)
    Try These Apps For The Road
    CD Players Awards – Q1 2013 (Part 2)
    CD Players Awards – Q1 2013 (Part 1)
    Home Cinema Amplifiers Awards – Q1 2013
    Most View
    Windows Server 2008 : Understanding Active Directory Sites (part 2)
    You Can Master RAW (Part 1)
    SQL Server 2008 : Developing with LINQ to SQL (part 1)
    Tips & Tricks: Keyboard Shortcuts
    Windows Phone 7 Development : Push Notifications - Implementing Cloud Service to Track Push Notifications
    Lenovo Thinkpad Xl Carbon – That’s What We’ve Been Waiting For
    Motorola Atrix 2 : Tricks With Atrix 2
    Best Photo Printers Revealed – Jan 2013 (Part 2) : Canon PIXMA MG6250
    Panasonic Super Zoom Lumix FZ200
    Excel 2013 – The Smartest Spreadsheet Software
    Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
    WCF Services : Data Contract - Equivalence
    Visual Studio 2010 IDE : Exporting Templates
    Speed up Linux (Part 3) - Enjoy better swappiness, The four-line speed boost
    Budget accessories to stylish PCs (Part 1) : Plantronics Marquee M155, TuneUp Utilities 2012, SteelSeries Sensei Mouse
    Windows Azure : Storing static reference data with dynamic data
    Happy iMas (Part 2)
    Programming COM+ Security (part 3) - Compiling and Installing the COM+ Application
    What Can Your Budget Buy? (Part 2)
    SQL Server 2008 : Using ADO.NET Data Services