观察CALayer中的动画属性变化 [英] Observing animated property changes in a CALayer

查看:94
本文介绍了观察CALayer中的动画属性变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CABasicAnimation可以为CALayer的属性设置动画. bounds.origin.我希望能够观察到属性随时间的变化,但还没有真正找到一种可以100%起作用的方法.

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. 我尝试在presentationLayerbounds.origin密钥路径上使用KVO(键值观察).系统抱怨对象在其观察者被释放之前就被释放了,这使我认为表示层只是临时的.将presentationLayer.bounds.origin用作键路径无效.

  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.

我尝试在另一层上创建一个属性并将其设置为动画通过声明@property并将其设置为该层上的@dynamic.但是,只有在访问表示层(例如在动画的末尾)时才更改此新属性,而在动画运行时似乎没有更新.

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.

我在#2中的属性上使用了needsDisplayForKey,它确实会在动画期间触发更新,但是针对以下问题:

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

  • 仅当CALayer具有非零帧时才有效.由于该层可能是CAShapeLayer或子类,因此它的帧数可能为零.
  • 它看起来像为该层触发了setNeedsDisplay,但是由于我实际上不是在绘制该层而只是监视属性更改,所以我不想使它重绘.
  • 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.

我尝试安排了NSTimer,并在计时器回调样本中安排了presentationLayer.这也适用,但针对以下问题:

I tried scheduling an NSTimer, and within the timer callback sample the presentationLayer. This also works but for these issues:

  • 计时器可能与动画更新略有不同步.
  • 由于原始动画有时会被另一个动画抢占,因此很难真正使定时器在动画运行时以及仅在动画运行时触发.

有什么建议吗?所有这些都将在iPhoneOS 3.0/3.1上实现.

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

推荐答案

我认为您已经列举了所有可能性.实际上,我什至不知道#2和#3,我写了. ;-)

I think you've named all of the possibilities. In fact, I wasn't even aware of #2 and #3 and I wrote the book on Core Animation. ;-)

KVO不适用于这些属性.如果可以的话,这会很好,但是我相信这与所需的开销有关.该值将非常频繁地更新,并且必须回调任何观察者.

KVO is not available for these properties. Would be nice if it were, but I believe the reason for this has to do with the overhead it would take. The value would update very frequently and have to call back to any observers.

无论如何,我发现NSTimer是最可靠的方法,但是现在我不确定您所说的是什么.是什么让您认为计时器不同步?为什么仅在动画运行时才很难触发计时器?您不能只在计时器回调中检查所需的条件,然后在不满足条件时不执行任何操作吗?

Anyhow, I've found the NSTimer to be the most reliable approach, but now I'm not sure from what you've said. What makes you think that the timer is out of sync? Why is it difficult get the timer only to fire when the animation is running? Can't you just check for the condition you want in the timer callback and then do nothing if the condition is not met?

最好的问候.

这篇关于观察CALayer中的动画属性变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆