为什么 view.layer.backgroundColor = randomColor 不会动画化颜色变化?
Why view.layer.backgroundColor = randomColor won't animate the color change?
当我尝试设置 view.layer.backgroundColor 时,颜色会立即改变,但没有动画。
但是如果我创建一个 CALayer,将其添加为 view.layer 的子层,并更改该 CALayer 的 backgroundColor,我可以看到隐含的颜色变化动画。
谁能解释为什么更改 view.layer.backgroundColor
时没有动画?
\ add CALayer
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
self.colorLayer.backgroundColor = [UIColor blueColor].CGColor;
[self.view.layer addSublayer:self.colorLayer];
\change the color somewhere else
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; // can animate
self.view.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; // won't animate
原因是该层是视图的底层。该层没有隐式层动画。隐式图层动画仅用于(我们可能称之为)次要图层,即您自己添加的子图层。
来自Core Animation Programming Guide:
Because iOS views always have an underlying layer, the UIView class itself derives most of its data from the layer object directly. As a result, changes you make to the layer are automatically reflected by the view object as well. This behavior means that you can use either the Core Animation or UIView interfaces to make your changes.
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.
(强调已添加。)
当我尝试设置 view.layer.backgroundColor 时,颜色会立即改变,但没有动画。
但是如果我创建一个 CALayer,将其添加为 view.layer 的子层,并更改该 CALayer 的 backgroundColor,我可以看到隐含的颜色变化动画。
谁能解释为什么更改 view.layer.backgroundColor
时没有动画?
\ add CALayer
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
self.colorLayer.backgroundColor = [UIColor blueColor].CGColor;
[self.view.layer addSublayer:self.colorLayer];
\change the color somewhere else
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; // can animate
self.view.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; // won't animate
原因是该层是视图的底层。该层没有隐式层动画。隐式图层动画仅用于(我们可能称之为)次要图层,即您自己添加的子图层。
来自Core Animation Programming Guide:
Because iOS views always have an underlying layer, the UIView class itself derives most of its data from the layer object directly. As a result, changes you make to the layer are automatically reflected by the view object as well. This behavior means that you can use either the Core Animation or UIView interfaces to make your changes.
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.
(强调已添加。)