To access the standard properties inside the drawing method, you need to make a few changes during the animation.
Use initializer
When CoreAnimation performs your animation, it creates shadow copies of your layer, and each copy will be displayed in a different frame. To create such copies, it calls -initWithLayer: From the Apple Documentation :
If you implement user-level subclasses, you can override this method and use it to copy the values ββof instance variables to a new object. Subclasses should always refer to the implementation of the superclass.
Therefore, you need to implement -initWithLayer: and use it to manually copy the value of your property to a new instance, for example:
- (id)initWithLayer:(id)layer { if ((self = [super initWithLayer:layer])) {
Model layer access properties
A copy, in any case, takes place before the animation starts: you can see this by adding the NSLog call to -initWithLayer: As far as CoreAnimation knows, your property will always be zero. Moreover, the created copies are readonly, if you try to set self.myProperty from -drawInContext: when the method is called in one of the presentation copies, you get an exception:
*** Terminating app due to uncaught exception 'CALayerReadOnly', reason: 'attempting to modify read-only layer <MyLayer: 0x8e94010>' ***
Instead of setting self.myProperty you should write
self.modelLayer.myProperty = 42.0f
since modelLayer instead refers to the original instance of MyCustomLayer , and all copies of the presentation use the same model. Note that you should also do this when reading a variable, and not just when setting it. For completeness, you must also specify the presentationLayer property, which instead displays the current (copy) layer.
Pietro saccardi
source share