Home > Articles > Programming > General Programming/Other Languages

Basic Animations with Core Animation

  • Print
  • + Share This
Marcus Zarra and Matt Long explain Core Animation's flexibility in allowing you to achieve your application animation goals.
This chapter is from the book

Core Animation is a powerful and mature technology that enables you to create animations that are as simple as you like or as complex as you need. To perform simple animations on windows and views, Apple provides the animation proxy object that, when called, causes an implicit animation to play when some visual component such as the view frame, opacity, or location is changed. For basic layer animation, the CABasicAnimation class provides a way to animate between two values, a starting value and an ending value. In this chapter, we look at these most basic methods for implementing animation in your application.

The Simplest Animations

With Core Animation integrated into Cocoa, you can animate windows, views, and layers implicitly by simply setting the value of the parameter you are interested in animating to some new value. When using a layer (CALayer), all you need to do is set the value with a direct call. For example, if you want to change the bounds of a layer, you simply call [layer setBounds:newFrame] where layer is the CALayer object you've created and added to your layer tree and newFrame is a CGRect containing the values of the new bound's size and origin. When this code is run, the change to the bounds of the layer is animated using the default animation for the keypath "bounds."

Similarly, when using a window (NSWindow) or view (NSView), all you need to do is set the value of the window or view property using the animation proxy object. This means that instead of calling [view setFrame:newFrame] to set the view frame, for example, you instead call [[view animator] setFrame:newFrame]. The difference is that we have instructed the view's animator proxy object to set the property for us—which implicitly animates the value from the current value to the value specified in newFrame.

  • + Share This
  • 🔖 Save To Your Account