IOS观察动画过程中的一个UIView的帧变化 [英] ios observing change in frame of a UIView during animation

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

问题描述

我要观察变化的x坐标。我想跟踪在改变这个动画过程中在粒度级别,因为我想使另一个观点,即认为被动画可与接触互动的改变x坐标。我想要在接触确切点的变化。我想了解做在更高层次上像这样的最佳方式:

I want to observe changes to the x-coordinate of my UIView's origin that's being animated using animateWithDuration:delay:options:animations:completion. I want to track changes in the x-coordinate during this animation at a granular level because I want to make a change in interaction to another view that the view being animated may make contact with. I want to make that change at the exact point of contact. I want to understand the best way to do something like this at a higher level:

- 我应该在完成回调在接触点下一animateWithDuration?换句话说,第一animateWithDuration动画直到达到该x坐标,和动画的其余发生在完成回调

-- Should I next animateWithDuration in the completion call back at the point of contact? In other words, The first animateWithDuration animates until it hits that x-coordinate, and the rest of the animation takes place in the completion callback?

- 我应该使用NSNotification观察员和观察变化框架财产?该如何准确/颗粒是什么?我可以跟踪每一个变化为x?我应该这样做在一个单独的线程?

-- Should I use NSNotification observers and observe changes to the frame property? How accurate / granular is this? Can I track every change to x? Should I do this in a separate thread?

任何其他建议,将受到欢迎。我在寻找一个最佳实践。

Any other suggestions would be welcome. I'm looking for a best practice.

推荐答案

创建一个的NSTimer 有一些延迟,每次过后运行特定的选择器。结果在该方法中检查动画视图的框架,并与您的碰撞视图进行比较。

create a NSTimer with some delay and run particular selector after each time lapse.
In that method check the frame of animating view and compare it with your colliding view.

和确保您使用 presentationLayer帧,因为如果你访问view.frame动画时,它给人的目标框架是恒定的,通过了动画。

And make sure you use presentationLayer frame because if you access view.frame while animating, it gives the destination frame which is constant through out the animation.

CGRect animationViewFrame= [[animationView.layer presentationLayer] frame];

如果您不希望创建计时器,写一些延迟后自称各地0.01秒。有延迟。

If you don't want to create timer, write a selector which calls itself after some delay.Have delay around .01 seconds.

澄清 - >结果
比方说你有5secs的时间,你是从(0,0)动画的位置,以便(100,100)。假设你实现志愿这种看法结果的框架

CLARIFICATION->
Lets say you have a view which you are animating its position from (0,0) to (100,100) with duration of 5secs. Assume you implemented KVO to the frame of this view

在调用 animateWithDuration块,然后观变化的直接的位置(100,100),这是最后的价值,即使视图移动与中间位置的值。结果

When you call the animateWithDuration block, then the position of the view changes directly to (100,100) which is final value even though the view moves with intermediate position values.

所以,您的志愿将一次性在动画开始的即时解雇。
因为,层具有图层树 presentation树。而图层树只是存储目的地值,同时 presentation层专卖店的中间值。结果
当您访问 view.frame 它总是给人帧的图层树不是取中间帧的值。

So, your KVO will be fired one time at the instant of start of animation. Because, layers have layer Tree and Presentation Tree. While layer tree just stores destination values while presentation Layer stores intermediate values.
When you access view.frame it will always gives the value of frame in layer tree not the intermediate frames it takes.

所以,你必须使用 presentation层帧来获得中间帧。

So, you had to use presentation Layer frame to get intermediate frames.

希望这有助于。

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

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