测试 UIView 是否处于动画中间
有什么方法可以判断 UIView 是否处于动画中间?当我在移动时打印视图对象时,我注意到有一个“动画”条目:
search bar should end editing: <UISearchBar: 0x2e6240; frame = (0 0; 320 88); text = ''; autoresize = W+BM; animations = { position=<CABasicAnimation: 0x6a69c40>; bounds=<CABasicAnimation: 0x6a6d4d0>; }; layer = <CALayer: 0x2e6e00>>
当动画停止并打印视图时,“动画”条目现在消失了:
search bar should end editing: <UISearchBar: 0x2e6240; frame = (0 0; 320 88); text = ''; autoresize = W+BM; layer = <CALayer: 0x2e6e00>>
Is there any way to tell if a UIView is in the middle of an animation? When I print out the view object while it is moving I notice that there is an "animations" entry:
search bar should end editing: <UISearchBar: 0x2e6240; frame = (0 0; 320 88); text = ''; autoresize = W+BM; animations = { position=<CABasicAnimation: 0x6a69c40>; bounds=<CABasicAnimation: 0x6a6d4d0>; }; layer = <CALayer: 0x2e6e00>>
When the animation has stopped and I print the view, the "animations" entry is now gone:
search bar should end editing: <UISearchBar: 0x2e6240; frame = (0 0; 320 88); text = ''; autoresize = W+BM; layer = <CALayer: 0x2e6e00>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
UIView
有一个层 (CALayer
)。您可以发送animationKeys
给它,这会给你一个键数组它标识附加到图层的动画。我想如果有任何条目,则动画正在运行。如果您想更深入地了解,请查看CALayer
采用的CAMediaTiming
协议。它提供了有关当前动画的更多信息。重要:如果您使用
nil
键添加动画 ([layer addAnimation:animation forKey:nil]
),animationKeys
code> 返回nil
。A
UIView
has a layer (CALayer
). You can sendanimationKeys
to it, which will give you an array of keys which identify the animations attached to the layer. I suppose that if there are any entries, the animation(s) are running. If you want to dig even deeper have a look at theCAMediaTiming
protocol whichCALayer
adopts. It does some more information on the current animation.Important: If you add an animation with a
nil
key ([layer addAnimation:animation forKey:nil]
),animationKeys
returnsnil
.iOS 9+ 方法,即使在
layer.animationKeys
不包含任何键时也能工作:来自文档:
iOS 9+ method, works even when
layer.animationKeys
contains no keys:From the docs:
动画实际上附加到底层核心动画
CALayer
类所以我认为你可以检查
myView.layer.animationKeys
Animations are attached in fact to the underlying Core Animation
CALayer
classSo I think you can just check
myView.layer.animationKeys
这里有很多过时的答案。我只需要防止
UIView
动画在同一个视图上运行时启动,因此我在UIView
上创建了一个类别:-这样我就可以检查任何视图的动画状态如下所示:-
这似乎比使用状态变量跟踪它更脆弱。
There are a lot of out-of-date answers here. I just needed to prevent a
UIView
animation being started if there was one running on the same view, so I created a category onUIView
:-This way I can just check any view's animation status like this:-
This seems less fragile than keeping track of it with a state variable.
我不确定问题的上下文,但我试图在开始第二个动画之前找出视图是否正在动画以避免跳过。然而,有一个 UIView 动画选项 UIViewAnimationOptionBeginFromCurrentState 可以在必要时组合动画以提供平滑的外观。从而消除了我需要知道视图是否有动画的需要。
I'm not sure of the context of the question but I had was attempting to find out if a view was animating before starting a second animation to avoid skipping. However there is a UIView animation option
UIViewAnimationOptionBeginFromCurrentState
that will combine the animations if necessary to give a smooth appearance. Thereby eliminating my need to know if the view was animating.动画键技巧有一个问题。
有时,动画完成后可能会有一些动画键徘徊。
这意味着非动画层可以返回一组animationKeys,即使它实际上没有动画。
您可以通过将动画的removedOnCompletion属性设置为YES来确保自动删除animationKeys。
例如,
如果您对应用于图层的所有动画执行此操作,它将确保当图层没有动画时不存在任何动画键。
There is a hitch with the animationKeys trick.
Sometimes there could be a few animationKeys lingering after an animation is completed.
This means that a non-animating layer could return a set of animationKeys even if it isn't actually animating.
You can make sure that animationKeys are automatically removed by setting an animation's removedOnCompletion property to YES.
e.g.
If you do this for all the animations you apply to your layer, it will ensure that when the layer isn't animating there are no animationKeys present.
其中一些对我不起作用。原因是这些动画是异步的。
我所做的是定义一个属性
并在我的动画中
Some of these didn't work for me. The reason for that is that these animations are asynchronous.
What I did is defined a property
And in my animation
参考问题: 动画期间的 UIView 中心位置
我比较视图的框架和图层。 presentation()?.frame 来检查它是否有动画。如果 leftView 即将结束,则 leftView.layer.presentation()?.frame 不等于其帧:
但是如果视图在动画期间移动到结束位置,则此方法可能不起作用。可能需要更多的条件检查。
Ref to the question: UIView center position during animation
I compare the view's frame and layer.presentation()?.frame to check it is animating. If leftView is on the way to finish, the leftView.layer.presentation()?.frame does not equal to its frame:
But this method may not work if the view move to the end position during the animation. More condition check may be necessary.
您可以按照此处的建议查询表示层 即使我没有动画,我的表示层也与我的模型层不匹配
You could query the presentation layer as suggested here My presentation layer does not match my model layer even though I have no animations
您可以使用 UIView 的图层属性。 CALayer有一个叫做动画键的属性,你可以检查它的计数是否大于0。
在文档中:
You can use the layer property of a UIView. CALayer has a property called animation keys, you can check its count if it is greater than 0.
In the Documentation: