Using CAKeyframeAnimation

Now that we have looked at how to coordinate the timing of separate animations, we will look at a method provided by Core Animation that enables us to control the timing and values within a single animation.

The API of CAKeyframeAnimation exposes two properties, each of which is expressed as an array of arbitrary length, specifying the values through which an animation passes.

Add the following method to the ViewController class:

func keyframeShapeAnimation()   {     let kfaY = CAKeyframeAnimation(keyPath: "position.y")     kfaY.values = [shapeLayer.position.y,                   80.0, 30.0, 60.0, 30.0, 120.0,                   layer.position.y]     kfaY.duration = 10.0     shapeLayer.add(kfaY, forKey: nil)   } 

The basic syntax of the preceding code shows some ...

Get Mastering macOS Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.