什么时候在iOS中发生隐式动画? [英] When exactly do implicit animations take place in iOS?

查看:348
本文介绍了什么时候在iOS中发生隐式动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人和每本书都声称CALayer中存在隐式动画。但是,每次我想验证到目前为止,我最终都会严格按照设定值进行操作。完全没有动画。

Everyone and every book claims that there are implicit animations happening in CALayer. However, every time I wanted to verify that so far, I end up with a hard-snap to the set value. No animation at all.

这是项目中没有其他任何事情发生的例子。我所做的只是创建一个视图,然后获取它的CALayer实例并执行一些应该隐式动画的内容。

Here's an example in a project where nothing else is happening. All I do is create a view, then get it's CALayer instance and do something that should be implicitly animated.

[theLayer setValue:[NSNumber numberWithFloat:M_PI * 1.1] forKeyPath:@"transform.rotation.z"];

另一个:

CGRect currentBounds = theLayer.bounds;
currentBounds.size.width += 120.f;
[self.animatedLayer setBounds:currentBounds];

该视图当然包含一些内容,因此我可以看到更改。
我看到了视觉上的变化,但作为一个难以捕捉。根本没有动画。

The view contains some stuff of course so I can see the change. I see the visual change, but as a hard snap. No animation at all.

因此,在撰写关于核心动画和隐式动画的内容时,要么所有这些书都是错误的,并且考虑到旧的Mac OS X知识,或者我正在做有问题。任何人都可以提供一个演示iPhone上隐式动画的工作示例吗?

So either all those books are wrong and have old Mac OS X knowledge in mind when writing about Core Animation and implicit animations, or I'm doing something wrong. Can anyone provide an working example that demonstrates implicit animations on the iPhone?

推荐答案

UIKit禁用隐式动画。更具体地说,与UIView相关联的CALayer永远不会隐式地设置动画。您自己创建且与UIView无关的CALayers将购买正常的隐式动画机制。

UIKit disables implicit animations. To be more specific, a CALayer associated with a UIView will never implicitly animate. CALayers that you create yourself and that are not associated with a UIView will buy into the normal implicit animation machinery.

如果您对其工作方式感兴趣,则会发生隐式动画在正常的-actionForKey:lookup之后。如果给定属性有CAAction,则使用它。否则,使用隐式动画。对于与UIView关联的CALayer,UIView实现 -actionForLayer:forKey:委托方法,当不在UIView动画块中时,它总是返回 [NSNull null] 表示动作查找应该在此处停止。这可以防止隐式动画工作。在UIView动画块内部,它构造自己的动作来表示当前的UIView动画设置。同样,这可以防止隐式动画。

If you're interested in how this works, implicit animations happen after the normal -actionForKey: lookup. If there's a CAAction for a given property, it's used. Otherwise, the implicit animation is used. In the case of a CALayer associated with a UIView, UIView implements the -actionForLayer:forKey: delegate method, and when not in a UIView animation block it always returns [NSNull null] to signify that the action lookup should stop here. This prevents implicit animations from working. Inside of a UIView animation block, it constructs its own action to represent the current UIView animation settings. Again, this prevents implicit animations.

如果您需要为UIView不为您做的CALayer属性设置动画,您可以使用显式CAAnimation子类(例如CABasicAnimation) 。

If you need to animate CALayer properties that UIView won't do for you, you can use an explicit CAAnimation subclass (such as CABasicAnimation).

这篇关于什么时候在iOS中发生隐式动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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