为什么文档表明CALayer动画必须在UIView动画块中? [英] Why do docs indicate CALayer animations must be in UIView animation blocks?

查看:144
本文介绍了为什么文档表明CALayer动画必须在UIView动画块中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Apple的核心动画指南,我在iOS中找到了关于图层支持视图的以下段落:

I am currently reading Apple's Core Animation Guide, where I found the following passage regarding layer-backed views in iOS:


如果要使用Core Animation类来启动动画,则必须从基于视图的动画块中发出所有Core Animation调用。 UIView类默认禁用图层动画,但在动画块内重新启用它们。因此,您在动画块之外所做的任何更改都不会动画。

If you want to use Core Animation classes to initiate animations, you must issue all of your Core Animation calls from inside a view-based animation block. The UIView class disables layer animations by default but reenables them inside animation blocks. So any changes you make outside of an animation block are not animated.

在引号下方,文档包含以下代码清单:

Just below the quote, the documentation includes the following code listing:

[UIView animateWithDuration:1.0 animations:^{
   // Change the opacity implicitly.
   myView.layer.opacity = 0.0;

   // Change the position explicitly.
   CABasicAnimation* theAnim = [CABasicAnimation animationWithKeyPath:@"position"];
   theAnim.fromValue = [NSValue valueWithCGPoint:myView.layer.position];
   theAnim.toValue = [NSValue valueWithCGPoint:myNewPosition];
   theAnim.duration = 3.0;
   [myView.layer addAnimation:theAnim forKey:@"AnimateFrame"];
}];

这意味着 CALayer s支持 UIView s必须放在动画块中。

which implies that both implicit and explicit animations on CALayers backing UIViews must be placed within an animation block.

但是,我发现这是显然是不真实的。具体来说,我已经使用 UIView 动画块之外的Core Animation类成功实现了显式动画。

However, I have found this to be patently untrue. Specifically, I have successfully implemented explicit animations using Core Animation classes outside of a UIView animation block.

我是否误解了这段经文,或者它是否已过时,还是其他什么?

Have I misunderstood this passage, or is it out-of-date, or something else?

一些额外的注释:

我假设UIView类默认禁用图层动画但重新启用它们在动画块中指的是 + [UIView setAnimationsEnabled:] 方法。当我回到可以这样做的计算机时,我会检查 + [UIView areAnimationsEnabled] 是否返回 YES

I assume that "the UIView class disables layer animations by default but reenables them inside animation blocks" refers to the +[UIView setAnimationsEnabled:] method. When I get back to a computer that can do so, I'll check to see whether +[UIView areAnimationsEnabled] returns YES or NO.

推荐答案

该引用是指支持视图的图层。对于您自己创建和管理的独立层,情况并非如此。

That quote refers to the layer that is backing the view. It is not true for stand-alone layers that you create and manage yourself.

iOS上的每个视图都有一个图层支持。更改视图的属性时,它会更改基础图层属性。默认情况下,图层将具有隐式动画,但除了在UIView动画块内部时,图层禁用该行为。这就是文档的这一部分所指的内容。

Every view on iOS is backed by a layer. When you change the view's properties, it changes the underlying layer property. By default the layer would have implicit animations, but the layer "disables" that behavior except for when you are inside of an UIView animation block. This is what that part of the documentation is referring to.

有两种方法可以使用Core Animation为图层属性设置动画。最常见的是在想要为属性设置动画时将动画对象添加到图层,但您也可以通过 actions 字典和 styles dictionary如果您总是想要在属性更改时进行动画处理。对于支持视图的图层,也会禁用最后两个。

There are a couple of ways you can use Core Animation to animate a layer property. The most common is to add the animation object to the layer when you want to animate the property, but you can also make customizations through the actions dictionary and the styles dictionary if you always want to animate when a property changes. The last two would also be disabled for the layer that is backing a view.

这篇关于为什么文档表明CALayer动画必须在UIView动画块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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