CALayer动画帧变化?

时间:2012-08-24 04:27:40

标签: ios calayer

我有一个CALayer,我已添加到我的观点中:

myView.myCALayer = [[CALayer alloc] init];
CGSize size = myView.frame.size;
myView.myCALayer.frame = CGRectMake(0, 0, size.width, size.height);
myView.myCALayer.backgroundColor = [[UIColor blackColor] CGColor];
[myView.layer addSublayer:myView.myCALayer];

当我在更改myView的框架后尝试更改CALayer的框架时,CALayer的大小调整为动画。我没有给CALayer添加任何动画,所以我不明白这一点。我甚至试图在调整大小之前调用图层上的removeAllAnimations,它仍然可以调整大小。

任何人都知道这里会发生什么?

2 个答案:

答案 0 :(得分:38)

为CALayer设置一些值时实际上存在隐式动画。您必须在设置新帧之前禁用动画。

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

[myView.myCALayer.frame = (CGRect){ { 10, 10 }, { 100, 100 } ];

[CATransaction commit];

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW3

答案 1 :(得分:5)

在Swift 4中:

CATransaction.begin()
CATransaction.setDisableActions(true)
/* Your layer / frame changes here */
CATransaction.commit()
相关问题