17.14. Animating and Moving Views


You want to animate the displacement of views.


Use the animation methods of UIView while displacing your views.


There are various ways of performing animations in iOS: capabilities are provided at a relatively low level, but also at a higher level. The highest level we can get is through UIKit, which is what we will be discussing in this section. UIKit includes some low-level Core Animation functionalities and presents us with a really clean API to work with.

The starting point for performing animations in UIKit is to call the beginAnimations:context: class method of the UIView class. Its first parameter is an optional name that you choose for your animation, and the second is an optional context that you can retrieve later to pass to delegate methods of the animations. We will talk about these shortly.

After you start an animation with the beginAnimations:context: method, it won’t actually take place until you call the commitAnimations class method of UIView class. The calculation you perform on a view object (such as moving it) between calling beginAnimations:context: and commitAnimations will be animated after the commitAnimations call. Let’s have a look at an example.

As we saw in Recipe 17.4, I included in my bundle an image called Xcode.png. This is Xcode’s icon, which I found by searching in Google Images (see Figure 17-14). Now, in my view controller (see Recipe 17.0), I want to place this image in an image view ...

Get iOS 6 Programming Cookbook now with O’Reilly online learning.

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