Objective-c – Observing animated property changes in a CALayer

calayercore-animationiphonemacosobjective c

I have a CABasicAnimation that animating a property of a CALayer e.g. bounds.origin. I want to be able to observe the property changing over time, but haven't really found a method that works 100%.

  1. I tried using KVO (key-value observation) on the presentationLayer's bounds.origin keypath. The system complains that the object is freed before its observers are freed, leading me to think that the presentation layer is only temporary. Observing presentationLayer.bounds.origin as a keypath doesn't work.

  2. I tried creating a property on another layer and animating that e.g. by declaring the @property and making it @dynamic on that layer. However this new property only gets changed when the presentation layer is accessed (e.g. at the end of the animation), it doesn't seem to update while the animation is running.

  3. I used needsDisplayForKey on the property in #2, which does trigger updates during the animation, but for these issues:

    • it only works if the CALayer has non-zero frame. Since this layer might be a CAShapeLayer or subclass, it may have a zero frame.
    • it looks like it triggers setNeedsDisplay for that layer, but since I'm not actually drawing that layer only monitoring the property change, I don't want to cause it to redraw.
  4. I tried scheduling an NSTimer, and within the timer callback sample the presentationLayer. This also works but for these issues:

    • The timer would probably be slightly out of sync with the animation update.
    • Since occasionally the original animation gets pre-empted by another animation, it's difficult to actually get the timer to fire when the animation is running and only when the animation is running.

Any suggestions? All this would be on iPhoneOS 3.0/3.1.

Best Answer

Try using CADisplayLink, which is designed to stay in sync with the animation loop. More info: https://ashfurrow.com/blog/animating-views-with-cadisplaylink/

Related Topic