Implementing the testDrive Method

In Chapter 9, I tell you how to create an action for the Test Drive button using Interface Builder, which generated a method stub for you. Now it’s time to fill that stub with code.

Add the bolded code in Listing 10-2 to the testDrive: method in TestDriveController.m. I’m also having you add the stubs for code you’ll be adding later so that you can run your program before you’re completely finished with the back and forth of the animation.

Listing 10-2: Updating testDrive: to Move the Car Up the Screen

- (IBAction)testDrive:(id)sender {

CGPoint center = CGPointMake(car.center.x,

self.view.frame.origin.y + car.frame.size.height/2);

[UIView animateWithDuration:3 animations:^ {

car.center = center;

}

completion:^(BOOL finished){

[self rotate];

}];

}

- (void)rotate {

}

- (void)returnCar {

}

- (void)continueRotation {

}

Now, run your program and click or touch the Test Drive button. You’ll see your car move up the screen. You’re on your way!

Looking more closely at Listing 10-2, you see that you start by creating the coordinate (CGPoint) of where you would like the car to end up.

remember.eps A car is just another view.

CGPoint center = CGPointMake(car.center.x,

self.view.frame.origin.y + car.frame.size.height/2);

You use the center and frame properties primarily for manipulating the view. If you’re changing only the position of the view (and not ...

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.