When I set the backgroundColor
property of an CALayer
instance, the change seems to be slightly animated. But I don't want that in my case. How can I set the backgroundColor
without animation?

- 29,441
- 10
- 93
- 100

- 40,201
- 65
- 225
- 373
-
possible duplicate of [How to disable CALayer implicit animations?](http://stackoverflow.com/questions/5833488/how-to-disable-calayer-implicit-animations) – Warpling Aug 03 '15 at 00:57
7 Answers
You can wrap the change in a CATransaction
with disabled animations:
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
//change background colour
[CATransaction commit];
-
3Any way to disable it completely for every future property setting in CALayer? I'm changing some properties like 20 times per second... – openfrog Oct 07 '10 at 17:49
-
I think you'll have to wrap your changes in these blocks to stop the implicit animations. I believe there might be a way to stop them by changing the delegate of the CALayer to nil but I'm not sure that method is supported if it even worked. – Ben Oct 07 '10 at 17:52
-
5Note for any future viewers: the answer posted [here](http://stackoverflow.com/questions/2244147/disabling-implicit-animations-in-calayer-setneedsdisplayinrect) offers much better performance that this one. – Matt Wilding Jul 10 '12 at 21:51
Objective C:
[CATransaction begin];
[CATransaction setDisableActions:YES];
// your code here
[CATransaction commit];
Swift:
CATransaction.begin()
CATransaction.setDisableActions(true)
// your code here
CATransaction.commit()

- 9,217
- 3
- 42
- 59
Swift
There are a couple other Swift answers here already, but I think this is the most basic answer:
CATransaction.begin()
CATransaction.setDisableActions(true)
// change the layer's background color
CATransaction.commit()

- 484,302
- 314
- 1,365
- 1,393
Try giving your layer a delegate
and then have the delegate
implement:
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)key {
return [NSNull null];
}

- 29,441
- 10
- 93
- 100

- 201
- 1
- 1
If you want to disable implicit animations for a property completely, you can do so by assigning to the actions
dictionary:
myLayer.actions = @{@"backgroundColor": [NSNull null]};
If you wish to disable the implicit animation on a case by case basis, then using [CATransaction setDisableActions:YES]
is still the better way to do it.

- 49,547
- 13
- 120
- 153
-
The two alternatives does different things. A transaction will disable all impact animations temporarily. Adding NSNull to the actions dictionary will disable implicit animations for that key path permanently (or until the dictionary changes). Neither solution is "simpler" than the other. They are just good for two different things. – David Rönnqvist Apr 21 '15 at 15:24
-
Explicitly for this question, this is indeed simpler because the actual goal was to permanently disable the animation (see comment to top answer). Since both solutions work (either *always* wrap in a `CATransaction` to turn them off, *or* simply assign the `actions` dictionary), I would argue that the one I propose is actually simpler. – Blixt Apr 21 '15 at 15:31
-
Yes, but the phrasing made it sound like it's generally simpler. I just wanted to provide the additional information, comparing the two. – David Rönnqvist Apr 21 '15 at 15:34
-
@DavidRönnqvist Okay, I changed the language to mention both options to avoid confusion. – Blixt Apr 21 '15 at 15:41
I've taken Ben's answer and made a Swift helper function, here it is in case it'll be useful for anyone:
func withoutCAAnimations(closure: () -> ()) {
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
closure()
CATransaction.commit()
}
# Example usage:
withoutCAAnimations { layer.backgroundColor = greenColor; }

- 1,914
- 20
- 29
Stop implicit animation on a layer in swift:
override func actionForLayer(layer: CALayer, forKey event: String) -> CAAction? {
return NSNull()
}
Remember to set the delegate of your CALayer instance to an instance of a class that at least extends NSObject. In this example we extend NSView.

- 5,355
- 43
- 38