Disabling animation when changing layer/view properties?

前端 未结 2 1234
心在旅途
心在旅途 2021-02-14 20:27

I composed a kind of animation by adding several layers to a UIView. Those layers shall be set visible or invisible by a script.

The script is based on objects that impl

相关标签:
2条回答
  • 2021-02-14 20:32

    Just wrap the code where you are making the change.

    [CATransaction begin];
    [CATransaction setAnimationDuration:0];
    
    [thelayer setAlpha:0];
    
    [CATransaction commit];
    
    0 讨论(0)
  • 2021-02-14 20:54

    Changing one of a layer's "animatable" properties creates what Apple's docs calls an implicit animation.

    To quote the Xcode docs on the subject:

    Core Animation’s implicit animation model assumes that all changes to animatable layer properties should be gradual and asynchronous. Dynamically animated scenes can be achieved without ever explicitly animating layers. Changing the value of an animatable layer property causes the layer to implicitly animate the change from the old value to the new value. While an animation is in-flight, setting a new target value causes the animation transition to the new target value from its current state.

    Under the covers, the system generates a CAAnimation that makes the change.

    As the other poster said, you can use setAnimationDuration to make the animation happen in an instant, which has the effect of turning animations off. I suspect that the system still generates an animation however.

    The official way to turn off implicit layer animations is to use

    [CATransaction begin];
    [CATransaction setDisableActions: YES];
    //layer changes
    [CATransaction commit];
    

    Edit:

    In Swift 3, this code would look like this:

    CATransaction.begin()
    CATransaction.setDisableActions(true)
    //layer changes
    CATransaction.commit()
    
    0 讨论(0)
提交回复
热议问题