How to prevent CALayer from implicit animations?

后端 未结 7 1571
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-25 14:59

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

相关标签:
7条回答
  • 2020-12-25 15:06
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    //your code here        
    [CATransaction commit];
    
    0 讨论(0)
  • 2020-12-25 15:07

    Try giving your layer a delegate and then have the delegate implement:

    - (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)key {
        return [NSNull null];
    }
    
    0 讨论(0)
  • 2020-12-25 15:20

    You can wrap the change in a CATransaction with disabled animations:

    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    //change background colour
    [CATransaction commit];
    
    0 讨论(0)
  • 2020-12-25 15:20

    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; }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-25 15:23

    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()
    
    0 讨论(0)
提交回复
热议问题