You can simply change the path
property of a CAShapeLayer
and it will animate using implicit animation. A display link is unnecessary for animation as the core animation subsystem handles everything for you. A display link is for when you need a high performance/accuracy timer that is synchronized with the refresh rate of your display.
To change the path property of a CAShapeLayer
, you can build up the path using a UIBezierPath
object and then return its CGPath. Something like (in Swift)
let bezier = UIBezierPath()
bezier.moveToPoint(CGPointMake(100.0, 100.0))
bezier.addLineToPoint(CGPointMake(150.0, 90.0))
...
bezier.closePath()
let shapeLayer = CAShapeLayer()
shapeLayer.path = bezier.CGPath
Keep in mind this is real code, however, you will need to build out the properties of your shape layer for display such as bounds and position, and at a minimum you'll want to set your shape layer's strokeColor
so you can see what's going on. The fillColor
property would be good to set too for this same reason.
Once you've established your initial shape (path), you can create a second bezier path that your path can animate to. Here's the rub, though. If your new path does not have the exact same number of points as your previous path, the animation likely won't do what you expect.
Coincidentally, I've been playing with this kind of thing lately just for fun. I wrote a little bit of code to see how it works. You can grab that (Swift) code here: https://github.com/perlmunger/Morph . It does what you see in this animation. It doesn't morph in exactly the way I would expect but it provides a neat illustration nonetheless: 