Search code examples
iosswiftcore-animationcabasicanimation

how to animation speed slow when animation stop


My code is below. How can I speed slow when animation has 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")
    }
}

Solution

  • Please find following details and add below line in your code,

    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.

    Hope this helps to you and let me know in case of any queries.