CABasicAnimation - transform scale keep in center

前端 未结 4 1347
南笙
南笙 2021-02-07 02:27

Trying to animatie an ellipse masked on a UIView to be scale transformed remaining in the center position.

I have found CALayer - CABasicAnimation not scaling around cen

4条回答
  •  旧时难觅i
    2021-02-07 02:40

    I have written following function with many added tweaks and options, could be useful for many others.

    func layerScaleAnimation(layer: CALayer, duration: CFTimeInterval, fromValue: CGFloat, toValue: CGFloat) {
        let timing = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
        let scaleAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
    
        CATransaction.begin()
        CATransaction.setAnimationTimingFunction(timing)
        scaleAnimation.duration = duration
        scaleAnimation.fromValue = fromValue
        scaleAnimation.toValue = toValue
        layer.add(scaleAnimation, forKey: "scale")
        CATransaction.commit()
    }
    

    Note: Don't forget to update size after animation completion, because CATransaction reverts back to actual size.

提交回复
热议问题