24
votes

I have a CALayer I've added to my view:

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];

When I attempt to change the frame of the CALayer after changing the frame of myView, the resize of the CALayer animates. I have added no animation to the CALayer, so I don't understand this. I've even attempted to call removeAllAnimations on the layer before the resize and it still animates the resize.

Anyone know what could be going on here?

2

2 Answers

43
votes

There is actually an implicit animation on setting some values for a CALayer. You have to disable the animations before you set a new frame.

[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

10
votes

In Swift 4:

CATransaction.begin()
CATransaction.setDisableActions(true)
/* Your layer / frame changes here */
CATransaction.commit()