我使用这段代码来旋转任何 View :
func rotateAnimation(theView: UIView, duration: CFTimeInterval = 20.0, repeatCount: Float = 200, toValue: CGFloat = CGFloat(.pi * 2.0)) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = toValue
rotateAnimation.duration = duration
rotateAnimation.repeatCount = repeatCount
theView.layer.add(rotateAnimation, forKey: nil)
}
我想知道如何加速当前正在运行的动画。例如,如果 UIView 正在使用上面的代码旋转,我怎样才能通过良好的转换使速度提高一倍?
因此:调用 rotateAnimation:以正常速度旋转 -> 调用 ?function? -> UIView 将平滑地更快地旋转到所需速度 -> UIView 将保持旋转所需的速度。
感谢您的帮助。
最佳答案
请尝试下面的代码
let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
self.viewToMove.layer.timeOffset = self.viewToMove.layer.convertTime(CACurrentMediaTime(), from: nil)
self.viewToMove.layer.beginTime = CACurrentMediaTime()
self.viewToMove.layer.speed += 0.5
}
timer.fire()
它会非常平滑地提高动画速度
关于ios - 平滑地加速正在运行的旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45892080/