How do you move a CALayer instantly (without animation)

前端 未结 5 1920
感情败类
感情败类 2020-12-12 12:51

I\'m trying to drag a CALayer in an iOS app.

As soon as I change its position property it tries to animate to the new position and flickers all over the

相关标签:
5条回答
  • 2020-12-12 13:01

    Combining previous answers here for Swift 4, to clearly make the animation duration explicit...

    extension CALayer
    {
        class func perform(withDuration duration: Double, actions: () -> Void) {
            CATransaction.begin()
            CATransaction.setAnimationDuration(duration)
            actions()
            CATransaction.commit()
        }
    }
    

    Usage...

    CALayer.perform(withDuration: 0.0) {
                aLayer.frame = aFrame
            }
    
    0 讨论(0)
  • 2020-12-12 13:06

    Swift 3 Extension :

    extension CALayer {
        class func performWithoutAnimation(_ actionsWithoutAnimation: () -> Void){
            CATransaction.begin()
            CATransaction.setValue(true, forKey: kCATransactionDisableActions)
            actionsWithoutAnimation()
            CATransaction.commit()
        }
    }
    

    Usage :

    CALayer.performWithoutAnimation(){
        someLayer.position = newPosition
    }
    
    0 讨论(0)
  • 2020-12-12 13:14

    You can also use the convenience function

    [CATransaction setDisableActions:YES] 
    

    as well.

    Note: Be sure to read the comments by Yogev Shelly to understand any gotchas that could occur.

    0 讨论(0)
  • 2020-12-12 13:18

    You want to wrap your call in the following:

    [CATransaction begin]; 
    [CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
    layer.position = CGPointMake(x, y);
    [CATransaction commit];
    
    0 讨论(0)
  • As others have suggested, you can use CATransaction.
    The problem comes arises because CALayer has a default implicit animation duration of 0.25 seconds.

    Thus, an easier (in my opinion) alternative to setDisableActions is to use setAnimationDuration with a value of 0.0.

    [CATransaction begin];
    [CATransaction setAnimationDuration:0.0];
    layer.position = CGPointMake(x, y);
    [CATransaction commit];
    
    0 讨论(0)
提交回复
热议问题