O'Reilly logo

Programming iOS 4 by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required