For a CALayer
,
which is animating,
class Test: CAGradientLayer {
override func draw(in ctx: CGContext) {
super.draw(in: ctx)
startPoint = ....
}
*** Terminating app due to uncaught exception 'CALayerReadOnly', reason: 'attempting to modify read-only layer
It appears to be impossible to change one of the ordinary animatable properties, inside the draw#inContext
call.
So for example:
It's easy and simple to have an animatable custom property of your own, and then draw something based on that. Here's some code for a .progress
property,
https://stackoverflow.com/a/37470079/294884
while animating your .progress
property, it would be easy to imagine wanting to set other properties of the layer, using some formula based on the value of .progress
each frame.
However, you can not do it in the draw#in
function - how to do it ?
When CoreAnimation performs animations, it creates shadow copies of layer, and each copy will be rendered in a different frame. Copies are created by -initWithLayer:
. Copies created by this method are read-only. That's why You are geting readonly exception.
You can override this method to create own copies of required properties. For example:
override init(layer: Any) {
super.init(layer: layer)
// Check class
if let myLayer = layer as? CircleProgressLayer {
// Copy the value
startPoint = myLayer.startPoint
}
}
Instead of setting self.startPoint
, you should write self.modelLayer.startPoint = ...
because all the presentation copies share the same model.
Note that you must do this also when you read the variable, not only when you set it. For completeness, one should mention as well the property presentation, that instead returns the current (copy of the) layer being displayed.
Refered to Apple Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With