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 ca
[CATransaction begin];
[CATransaction setDisableActions:YES];
//your code here
[CATransaction commit];
Try giving your layer a delegate
and then have the delegate
implement:
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)key {
return [NSNull null];
}
You can wrap the change in a CATransaction
with disabled animations:
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
//change background colour
[CATransaction commit];
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; }
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.
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()