CABasicAnimation resets to initial value after animation completes

后端 未结 15 2511
闹比i
闹比i 2020-12-04 05:44

I am rotating a CALayer and trying to stop it at its final position after animation is completed.

But after animation completes it resets to its initial position.

15条回答
  •  时光取名叫无心
    2020-12-04 05:56

    So my problem was that I was trying to rotate an object on pan gesture and so I had multiple identical animations on each move. I had both fillMode = kCAFillModeForwards and isRemovedOnCompletion = false but it didn't help. In my case, I had to make sure that the animation key is different each time I add a new animation:

    let angle = // here is my computed angle
    let rotate = CABasicAnimation(keyPath: "transform.rotation.z")
    rotate.toValue = angle
    rotate.duration = 0.1
    rotate.isRemovedOnCompletion = false
    rotate.fillMode = CAMediaTimingFillMode.forwards
    
    head.layer.add(rotate, forKey: "rotate\(angle)")
    

提交回复
热议问题