动画停止时如何使动画速度变慢

how to animation speed slow when animation stop

我的代码如下。如何在动画停止时变慢?

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.fromValue = 0.0
        rotation.toValue = 25
        rotation.duration = 1.5
        rotation.isCumulative = true
        rotation.repeatCount = 1
        self.layer.add(rotation, forKey: "rotationAnimation")
    }
}

您可以使用self.layer.speed来平滑地降低动画速度

你可以这样做

注意:这里需要做一些修改 xcode

中没有测试
  let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
        self.layer.timeOffset = self.layer.convertTime(CACurrentMediaTime(), from: nil)
        if self.layer.speed == 0 { timer.invalidate() }
        self.layer.beginTime = CACurrentMediaTime()
        self.layer.speed -= 0.5

    }
    timer.fire()

希望对您有所帮助

请找到以下详细信息并在您的代码中添加以下行,

rotation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

EaseInOut An ease-in ease-out curve causes the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing. This is the default curve for most animations.

EaseIn An ease-in curve causes the animation to begin slowly, and then speed up as it progresses.

EaseOut An ease-out curve causes the animation to begin quickly, and then slow down as it completes.

希望这对您有所帮助,如有任何疑问,请告诉我。