How do you move a CALayer instantly (without animation)

前端 未结 5 1939
感情败类
感情败类 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
            }
    

提交回复
热议问题