Skip to content

CAKeyframeAnimation ​

CAKeyframeAnimation represents an animation that makes one of a layer's properties pass through several values in turn, or that moves a layer along a path. The values passed through are called keyframes.

As with toValue of CABasicAnimation, the type of the keyframe values is determined by keyPath, so keyPath must be set before values can be.

Inheritance: EventTarget → NSObject → CAAnimation → CAPropertyAnimation → CAKeyframeAnimation

For using it to blink, change colors, and move along a path, see the guide Animation.

Constructor ​

CAKeyframeAnimation()

Creates a keyframe animation with no property specified yet.

Static methods ​

CAKeyframeAnimation.animationWithKeyPath()

Creates a keyframe animation with a property specified.

Instance properties ​

Also inherits keyPath from CAPropertyAnimation and the properties of CAAnimation; the commonly used ones are duration, repeatCount, and autoreverses.

PropertyDescription
valuesThe values passed through in turn
pathThe layer moves along this path. When set, values has no effect
keyTimesWhen each value occurs
timingFunctionsThe timing curve used between each pair of adjacent values
calculationModeHow it transitions from value to value
rotationModeWhether the layer turns when moving along a path
tensionValuesThe tightness of the smooth curve near each value
continuityValuesThe continuity of the smooth curve near each value
biasValuesThe bias of the smooth curve near each value

Events ​

Inherits start and stop from CAAnimation.

Examples ​

js
const layer = new CALayer();
layer.frame = new CGRect(20, 70, 20, 20);
layer.backgroundColor = 'systemBlue';

const blink = CAKeyframeAnimation.animationWithKeyPath('opacity');
blink.values = [1, 0.2, 1, 0.2, 1];
blink.keyTimes = [0, 0.2, 0.5, 0.7, 1];
blink.duration = 1.5;
layer.addAnimation(blink, { forKey: 'blink' });

Effect: Once the layer is shown on screen, it blinks twice over 1.5 seconds: fades, recovers, fades again, recovers again. Not verified in the simulator.

Corresponding iOS API ​

ObjCJS
[CAKeyframeAnimation animationWithKeyPath:@"position"]CAKeyframeAnimation.animationWithKeyPath('position')
keyframe.values = @[@0, @1]keyframe.values = [0, 1]
keyframe.path = path.CGPathkeyframe.path = path
keyframe.calculationMode = kCAAnimationPacedkeyframe.calculationMode = 'paced'
keyframe.rotationMode = kCAAnimationRotateAutokeyframe.rotationMode = 'auto'

See also ​

  • Animating layers: passing through several values, switching values without interpolation, moving along a path
  • UIBezierPath: the paths used for moving along a path
  • UIImage: frame-by-frame animation from a set of images