I\'d like to animate a drawing sequence. My code draws a spiral into a UIImageView.image. The sequence changes the image contents, but also changes the scale
@Matt provided the answer and gets the checkmark. I'll recap some points for emphasis:
CABasicAnimation(keyPath:)
to it.myView.layer.addSublayer(myLayer)
override class func needsDisplay(forKey key: String) -> Bool
with your key property (as @Matt's example shows), and also override func draw(in cxt: CGContext)
to do your drawing. Be sure to decorate your key property with @objc
. And reference the key property within the drawing code.override func draw(_ rect: CGRect) { }
) to avoid conflict between animated and non-animated drawing on the separate layers. For coordinating animated and non-animated content in the same UIView, it's good (necessary?) to do all your drawing from your custom layer. myLayer.setNeedsDisplay()
to update the non-animated content within the custom layer; use myLayer.add(myBasicAnimation, forKey:nil)
to trigger animated drawing within the custom layer.As I said above, @Matt answered - but these items seemed worth emphasizing.