How to prevent CALayer from implicit animations?

后端 未结 7 1584
佛祖请我去吃肉
佛祖请我去吃肉 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: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; }
    

提交回复
热议问题