Core Animation provides an expressive set of animation classes you can use in your application:
-
CAAnimation
is the abstract class that all animations subclass.CAAnimation
adopts theCAMediaTiming
protocol which provides the simple duration, speed, and repeat count for an animation.CAAnimation
also adopts theCAAction
protocol. This protocol provides a standardized means for starting an animation in response to an action triggered by a layer.The
CAAnimation
class also defines an animation’s timing as an instance ofCAMediaTimingFunction
. The timing function describes the pacing of the animation as a simple Bezier curve. A linear timing function specifies that the animation's pace is even across its duration, while an ease-in timing function causes an animation to speed up as it nears its duration. -
CAPropertyAnimation
is an abstract subclass ofCAAnimation
that provides support for animating a layer property specified by a key path. -
CABasicAnimation
is a subclass ofCAPropertyAnimation
that provides simple interpolation for a layer property. -
CAKeyframeAnimation
(a subclass ofCAPropertyAnimation
) provides support for key frame animation. You specify the key path of the layer property to be animated, an array of values that represent the value at each stage of the animation, as well as arrays of key frame times and timing functions. As the animation runs, each value is set in turn using the specified interpolation. -
CATransition
provides a transition effect that affects the entire layer's content. It fades, pushes, or reveals layer content when animating. On Mac OS X, the stock transition effects can be extended by providing your own custom Core Image filters. -
CAAnimationGroup
allows an array of animation objects to be grouped together and run concurrently.
Figure 1 shows the animation class hierarchy, and also summarizes the properties available through inheritance.
Figure 1 Core Animation classes and protocolCAKeyframeAnimation
class, is similar to basic animation; however it allows you to specify an array of target values. Each of these target values is interpolated, in turn, over the duration of the animation.
Keyframe values are specified using one of two properties: a Core Graphics path (the path
property) or an array of objects (the values
property).
A Core Graphics path is suitable for animating a layer’s anchorPoint
or position
properties, that is, properties that are CGPoints
. Each point in the path, except for moveto
points, defines a single keyframe segment for the purpose of timing and interpolation. If the path
property is specified, the values
property is ignored.
By default, as a layer is animated along a CGPath it maintains the rotation to which it has been set. Setting therotationMode
property to kCAAnimationRotateAuto
or kCAAnimationRotateAutoReverse
causes the layer to rotate to match the path tangent.
Providing an array of objects to the values
property allows you to animate any type of layer property. For example:
-
Provide an array of
CGImage
objects and set the animation key path to thecontent
property of a layer. This causes the content of the layer to animate through the provided images. -
Provide an array of
CGRects
(wrapped as objects) and set the animation key path to theframe
property of a layer. This causes the frame of the layer to iterate through the provided rectangles. -
Provide an array of
CATransform3D
matrices (again, wrapped as objects) and set theanimation
key path to thetransform
property. This causes each transform matrix to be applied to the layer’stransform
property in turn.
Keyframe animation requires a more complex timing and pacing model than that of a basic animation.
The inherited timingFunction
property is ignored. Instead you can pass an optional array ofCAMediaTimingFunction
instances in the timingFunctions
property. Each timing function describes the pacing of one keyframe to keyframe segment.
While the inherited duration property is valid for CAKeyframeAnimation
, you can attain more subtle control of timing by using the keyTimes
property.
The keyTimes
property specifies an array of NSNumber
objects that define the duration of each keyframe segment. Each value in the array is a floating point number between 0.0 and 1.0 and corresponds to one element in the values
array. Each element in the keyTimes
array defines the duration of the corresponding keyframe value as a fraction of the total duration of the animation. Each element value must be greater than, or equal to, the previous value.
The appropriate values for the keyTimes
array are dependent on the calculationMode
property.
-
If the
calculationMode
is set tokCAAnimationLinear
, the first value in the array must be 0.0 and the last value must be 1.0. Values are interpolated between the specified keytimes. -
If the
calculationMode
is set tokCAAnimationDiscrete
, the first value in the array must be 0.0. -
If the calculationMode is set to kCAAnimationPaced, the keyTimes array is ignored.
The CATransition
class provides transition functionality to Core Animation. It is a direct subclass of CAAnimation
as it affects an entire layer, rather than a specific property of a layer.
A new instance of CATransition
is created using the inherited class method animation
. This will create a transition animation with the default values shown in Table 1:
Transition Property | Value |
---|---|
| Uses a fade transition. The value is |
| Not applicable. |
| Uses the duration of the current transaction or 0.25 seconds if the duration has not been set for a transaction. The value is 0.0 |
| Uses linear pacing. The value is |
| 0.0 |
| 1.0 |
Once created, you can configure the transition animation using one of the predefined transition types or, on Mac OS X, create a custom transition using a Core Image filter.
The predefined transitions are used by setting the type
property to one of the constants in Table 2.
Transition Type | Description |
---|---|
The layer fades as it becomes visible or hidden. | |
The layer slides into place over any existing content. | |
The layer pushes any existing content as it slides into place | |
The layer is gradually revealed in the direction specified by the transition subtype. |
With the exception of kCATransitionFade
, the predefined transition types also allow you to specify a direction for the transition by setting the subType
property to one of the constants in Table 3.
Transition Subtype Constant | Description |
---|---|
The transition begins at the right side of the layer. | |
The transition begins at the left side of the layer. | |
The transition begins at the top of the layer. | |
The transition begins at the bottom of the layer. |
The startProgress
property allows you to change the start point of the transition by setting a value that represents a fraction of the entire animation. For example, to start a transition half way through its progress the startProgress
value would be set to 0.5. Similarly, you can specify the endProgress
value for the transition. The endProgress
is the fraction of the entire transition that the transition should stop at. The default values are 0.0 and 1.0, respectively.
The CAKeyframeAnimation
class provides a powerful means of animating layer properties. However,CAKeyframeAnimation
does not allow you to specify a single animation timing function that is used for the entire path. Instead you are required to specify the timing using the keyTimes
property, or by specifying an array of timing functions in the timingFunctions
property.
You can provide a single timing function for the animation by grouping the keyframe animation in aCAAnimationGroup
, and setting the group animation’s timing function to the desired CAMediaTimingFunction
. The animation group’s timing function and duration take precedence over the keyframe animation’s timing properties.
A code fragment that implements this strategy is shown in Listing 1.
Listing 1 Using a single timing function for a keyframe animation
// create the path for the keyframe animation |
CGMutablePathRef thePath = CGPathCreateMutable(); |
CGPathMoveToPoint(thePath,NULL,15.0f,15.f); |
CGPathAddCurveToPoint(thePath,NULL, |
15.f,250.0f, |
295.0f,250.0f, |
295.0f,15.0f); |
// create an explicit keyframe animation that |
// animates the target layer's position property |
// and set the animation's path property |
CAKeyframeAnimation *theAnimation=[CAKeyframeAnimation |
animationWithKeyPath:@"position"]; |
theAnimation.path=thePath; |
// create an animation group and add the keyframe animation |
CAAnimationGroup *theGroup = [CAAnimationGroup animation]; |
theGroup.animations=[NSArray arrayWithObject:theAnimation]; |
// set the timing function for the group and the animation duration |
theGroup.timingFunction=[CAMediaTimingFunction |
functionWithName:kCAMediaTimingFunctionEaseIn]; |
theGroup.duration=15.0; |
// release the path |
CFRelease(thePath); |
// adding the animation to the target layer causes it |
// to begin animating |
[theLayer addAnimation:theGroup forKey:@"animatePosition"]; |