Animating a Series of Images “In Place”

Although I explain animation using the UIView methods earlier in this chapter, this section shows you a way to animate a series of images “in place” — you are not moving the image around as you did earlier with the car; instead you are changing the image where it is to make it appear as if it were animated.

To make the Test Drive button blink, for example, add the bolded code in Listing 10-15 to TestDriveController.m. As you can see in the listing, only a single line of code is needed to animate the button.

Listing 10-15: Creating a Blinking Button

- (void)viewDidLoad

{

[super viewDidLoad];

NSURL* backgroundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@”CarRunning” ofType:@”aif”]];

backgroundAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundURL error:nil];

backgroundAudioPlayer.numberOfLoops = -1;

[backgroundAudioPlayer prepareToPlay];

NSURL* burnRubberURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”BurnRubber” ofType:@”aif”]];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)burnRubberURL, &burnRubberSoundID);

[testDriveButton setBackgroundImage:[UIImage animatedImageNamed:@”Button” duration:1.0 ] forState:UIControlStateNormal];

}

In Chapter 5, I show you how to add a custom button with a Button background image. You could have also programmatically added the background image by sending the button the setBackgroundImage:forState: message. (Chapter 5 explains the ...

Get iPad Application Development For Dummies, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.