我想为UIImageView设置动画。图像只能顺时针旋转。当图像旋转时,我想加快速度。当我加快速度时,它会逆时针旋转,起始位置也不正确。这是我的代码:
func rotateAnimation(theView: UIView, duration: CFTimeInterval = 2.0, repeatCount: Float = 1, toValue: CGFloat = CGFloat(.pi * 2.0), linear: Bool = false, animation: String = "transform.rotation") {
let rotateAnimation = CABasicAnimation(keyPath: animation)
let currentLayer = (theView.layer.presentation())
var currentAngle = CFloat((currentLayer?.value(forKeyPath: "transform.rotation") as? NSNumber)!)
currentAngle = roundf(currentAngle * 180 / Float(Double.pi))
print(currentAngle)
rotateAnimation.fromValue = currentAngle
if !linear{
rotateAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
}
rotateAnimation.toValue = toValue
rotateAnimation.duration = duration
rotateAnimation.repeatCount = repeatCount
theView.layer.add(rotateAnimation, forKey: nil)
}
打印显示我纠正角度。
我在下面两次调用此方法。我第二次打电话给它,图像正在旋转:
rotateAnimation(theView: star, duration: 15.0, repeatCount: Float.infinity, toValue: CGFloat(Double.pi * 2), linear: true)
delay(delay: 3.5, closure: {
self.rotateAnimation(theView: self.star, duration: 7, repeatCount: Float.infinity, toValue: CGFloat(Double.pi * 2), linear: true)
})