An object that provides keyframe animation capabilities for a layer object.


You create a CAKeyframeAnimation object using the inherited init(keyPath:) method, specifying the key path of the property that you want to animate on the layer. You can then specify the keyframe values to use to control the timing and animation behavior.

For most types of animations, you specify the keyframe values using the values and keyTimes properties. During the animation, Core Animation generates intermediate values by interpolating between the values you provide. When animating a value that is a coordinate point, such as the layer’s position, you can specify a path for that point to follow instead of individual values. The pacing of the animation is controlled by the timing information you provide.

Listing 1 shows how to create a keyframe animation that animates a layer's background color from red to green to blue over a two second duration.

Listing 1

Animating a layer's background color with a keyframe animation

let colorKeyframeAnimation = CAKeyframeAnimation(keyPath: "backgroundColor")

colorKeyframeAnimation.values = [,
colorKeyframeAnimation.keyTimes = [0, 0.5, 1]
colorKeyframeAnimation.duration = 2


Providing keyframe values

var values: [Any]?

An array of objects that specify the keyframe values to use for the animation.

var path: CGPath?

The path for a point-based property to follow.

Keyframe timing

var keyTimes: [NSNumber]?

An optional array of NSNumber objects that define the time at which to apply a given keyframe segment.

var timingFunctions: [CAMediaTimingFunction]?

An optional array of CAMediaTimingFunction objects that define the pacing for each keyframe segment.

var calculationMode: String

Specifies how intermediate keyframe values are calculated by the receiver.

Rotation Mode Attribute

var rotationMode: String?

Determines whether objects animating along the path rotate to match the path tangent.

Cubic Mode Attributes

var tensionValues: [NSNumber]?

An array of NSNumber objects that define the tightness of the curve.

var continuityValues: [NSNumber]?

An array of NSNumber objects that define the sharpness of the timing curve’s corners.

var biasValues: [NSNumber]?

An array of NSNumber objects that define the position of the curve relative to a control point.


Rotation Mode Values

These constants are used by the rotationMode property.

Value calculation modes

These constants are used by the calculationMode property.


Inherits From