How to animate the frame of an layer with CABasicAnimation?

前端 未结 6 546
刺人心
刺人心 2020-12-05 07:57

I guess I have to convert the CGRect into an object to pass it to fromValue?

This is how I try it, but it doesn\'t work:

CABasicAnimation *frameAnima         


        
6条回答
  •  醉梦人生
    2020-12-05 08:02

    The question is antique, but I will answer it anyway.

    Frame property is not animatable. You have to animate other properties. Also you have to disable implicit animations.

        let updatedBounds = ...
        let animation = CABasicAnimation(keyPath: "bounds")
        animation.duration = 0.5
        //it's better to start animation from presentation layer in case there is already animation going on
        animation.fromValue = customLayer.presentation()?.bounds
        animation.toValue = updatedBounds
        customLayer.add(animation, forKey: nil)
    
        //disable implicit animation for thoose properties
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        //update properties so they will be updated at the end of animation
        customLayer.bounds = updatedBounds
        customLayer.position = originalRect.origin
        customLayer.anchorPoint = CGPoint(x: 0, y: 0)
        CATransaction.commit()
    

提交回复
热议问题