Rotating the Object

In this section, I show you how to rotate a view (in this case, turn the car around). To do so, you update the rotate code stub we started out with back in Listing 10-2 with the bolded code in Listing 10-4.

Listing 10-4: Updating rotate

- (void)rotate {

CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);

void (^animation)() = ^() {

car.transform = transform;


void (^completion)(BOOL) = ^(BOOL finished){

[self returnCar];


[UIView animateWithDuration:3 animations:animation completion:completion];


This method uses the block declarations I explain in the previous section.

The CGAffineTransform data structure represents a matrix used for affine transformations — a blueprint for how points in one coordinate system map to points in another coordinate system. Although CGAffineTransform has a number of uses (such as scaling and translating a coordinate system), the only one covered here is the rotation method you use in Listing 10-4:


To rotate a view, you specify the angle (in radians) to rotate the coordinate system axes. Whereas degrees are numbers between 0 and 360, radians, though similar, range from 0 to 2*pi. So when you create a rotation that turns an object around one half-circle, that rotation in radians is pi. (M_PI is a system constant that represents pi.)

tip.eps Just to make your life ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.