View Animation

Animation is ultimately layer animation. However, for a limited range of attributes, you can animate a UIView directly: these are its alpha, backgroundColor, bounds, center, frame, and transform. You can also animate a UIView’s change of contents. Despite the brevity of the list, UIView animation is an excellent way to become acquainted with animation and to experiment with the various parameters you can use to determine how an animation behaves; in many cases it will prove quite sufficient.

There are actually two ways to ask for UIView animation: the old way (before iOS 4.0, and still available), and the new way (iOS 4.0 and later only, because it uses Objective-C blocks; see Chapter 3). I’ll describe the old way first.

Animation Blocks

To animate a change to an animatable UIView property the old way, wrap the change in calls to the UIView class methods beginAnimations:context: and commitAnimations. Just to make life more confusing, the region between these calls is referred to as an animation block, even though it is not a block in the syntactical Objective-C sense.

So, animating a change to a view’s background color could be as simple as this:

[UIView beginAnimations:nil context:NULL];
v.backgroundColor = [UIColor yellowColor];
[UIView commitAnimations];

Any animatable change made within an animation block will be animated, so we can animate a change both to the view’s color and its position simultaneously:

[UIView beginAnimations:nil context:NULL]; v.backgroundColor ...

Get Programming iOS 4 now with O’Reilly online learning.

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