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 using animation blocks (before iOS 4.0, and still available), and the new way using Objective-C blocks (Chapter 3). I’ll describe the old way first; the documentation describes this approach as “discouraged,” but it is not officially deprecated and it does still work, and it may offer some advantages over the newer block-based animation.
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
. 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:nil]; v.backgroundColor = [UIColor yellowColor]; [UIView commitAnimations];
Any animatable change made within an animation block will be animated, so we ...
Get Programming iOS 6, 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.