Cover by Vandad Nahavandipoor

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

O'Reilly logo

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

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