在 UIView 动画期间观察键值 [英] Key value Observing during UIView Animations
问题描述
我正在为程序中的视图的 center 属性设置动画.在动画过程中,当中心达到特定值时我需要通知.
I'm animating the center property of a view in my program. During the animation, I need notifications when center hits a particular value.
我尝试将自己添加为 view 的 center 属性的键值观察者.但是,我只在动画开始时收到通知.所以我无法检测物体是否/何时通过我的兴趣点.
I tried adding the myself as a key value observer for the center property of the view . However, I only get notified when the animation begins. So I'm unable to detect if/when the object passes through my point of interest.
有没有办法通过 KVO 或任何其他方法来做到这一点?
Is there a way to do this through KVO or any other method?
谢谢!
推荐答案
您可以通过访问表示层来检索表示 UIView 动画层当前状态的值.这可以使用如下代码完成:
You can retrieve the values representing the current state of the UIView's animating layer by accessing its presentation layer. This can be done using code like the following:
CGPoint currentCenter = [[view.layer presentationLayer] center];
不幸的是,表示层的属性不符合 KVO,因此我能想到的跟踪当前值的最佳方法是不断轮询表示层,直到它靠近您想要的位置.
Unfortunately, the presentation layer's properties are not KVO-compliant, so the best way I can think of for tracking the current value is to keep polling the presentation layer until it gets near the location you want.
这篇关于在 UIView 动画期间观察键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!