20.14. Animating and Moving Views

Problem

You want to animate the displacement of views.

Solution

Use the animation methods of UIView while displacing your views.

Discussion

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 20.4, I included in my assets category an image called Xcode.png. This is Xcode’s icon, which I found by searching in Google Images (see Figure 20-9). Now, in my view controller (see this chapter’s Introduction), I want to place this image ...

Get iOS 7 Programming Cookbook 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.