如何暂停和恢复UIView动画?
我有一个带有多个 UILabel 的 UIView,它们从上到下进行动画处理,反之亦然。可以说是一种 Autoque :) 我使用 2 个函数:
-(void)goUp
-(void)goDown
这些函数将 UIView 动画启动到所需的位置。它们都定义了一个在最后调用另一个函数的 AnimationDidStopSelector 。这一切都很顺利。
当触摸屏幕时,使用touchesBegan,我想暂停当前动画并使用touchesMoved 事件更改UIView 的垂直位置。在 TouchesEnded 中,我想将动画恢复到所需的结束位置。
这样做的正确方法是什么?
托马斯
I have a UIView with several UILabels that is animating from top to bottom and vice versa. A sort of Autoque let's say :) I use 2 functions:
-(void)goUp
-(void)goDown
Those functions start a UIView animation to the required position.They both have an AnimationDidStopSelector defined that calls the other function at the end. This all works smoothly.
When touching the screen, using touchesBegan, I would like to pause the current animation and change the vertical position of the UIView using touchesMoved event. In touchesEnded I want to resume the animation to the required end position.
What would be the proper way to do so?
Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在 UIView 上创建了一个类别来暂停和停止动画:
I've created a category on UIView to pause and stop animations:
实际上,您仍然可以根据 Vladimir 链接到的问题的答案来暂停 UIView 动画,因为它会暂停我的 CABasicAnimations 以及 UIView > 在我将所有动画实现为
CABasicaAnimations
之后添加一些UIView
动画,我原以为不会暂停,但它们也不起作用。 这是相关链接。我想暂停整个视图,因此我传递了 self.view.layer 作为要暂停的图层。但对于那些不了解
CALayer
的人,请传入您想要暂停的view.layer
。每个UIView
都有一个CALayer
,因此只需传入与您相关的最上面的view.layer
即可。对于 Thomas 来说,根据您自己的回答,您似乎希望传入 self.containerView.layer 来暂停。之所以有效,是因为 UIView 动画只是核心动画之上的一层。至少这是我的理解。
希望这可以帮助未来想知道如何暂停动画的人。
Actually, you can still pause the
UIView
animations based on the answer to the question that Vladimir linked to as it pause myCABasicAnimations
as well as myUIView
animations after I had implemented all my animations asCABasicaAnimations
and then added someUIView
animations afterwards that I had thought wouldn't be paused, but they didn't work either. This is the relevant link.I wanted to pause my whole view so I passed
self.view.layer
as the layer to be paused. But for those who don't know aboutCALayer
's, pass in theview.layer
that you want paused. EachUIView
has aCALayer
, so just pass in the upmostview.layer
that is relevant for you. In the case of Thomas, based on your own answer, it would seem that you would want to pass inself.containerView.layer
to be paused.The reason that this works is because
UIView
animations are simply a layer on top of Core Animation. At least that's my understanding.Hope this helps future people wondering how to pause animations.
弗拉基米尔,关于 CAAnimations 的问题是有道理的..但我找到了一种“暂停”的方法,这样我就可以继续使用 UIView 动画:
我在这里所做的是使用presentationLayer 找出动画视图的当前 x 值。之后我执行一个新的动画来覆盖原始动画。确保为此设置 setAnimationBeginsFromCurrentstate:YES。这会取消原始动画,并将动画视图放置在动画过程的当前位置,而不是其目标位置(它会自动执行)。
希望这对其他人也有帮助! :)
Vladimir, the question about CAAnimations make sense.. but I found a way to 'pause' so I could keep using UIView animations:
What I'm doing here is using the presentationLayer to find out current x value of the animated view. After that I execute a new animation which overwrites the original animation. Make sure to setAnimationBeginsFromCurrentstate:YES for this. This cancels the original animation and puts the animated view not to it's destination position (which it does automatically ) but at the current position of the animation proces..
Hope this helps others too! :)
我不确定是否可以直接使用 UIView,但你绝对可以使用动画视图的 CALayers 来代替。请参阅关于暂停和恢复 CAAnimations 的此问题。
I'm not sure if it is possible with UIView's directly but you definitely can do that animating view's CALayers instead. See this question about pausing and resuming of CAAnimations.
希望它能帮助你。
当您调用图层动画时,它将影响所有图层树和子模式图层动画。
Hope it will help you.
When you call the Layer animate, it will effect all layer tree and submode layer animate.