我在Swift中开发iOS应用程序,我想用360度旋转图像的运动。动画应在每轮完成后停止2秒。快速图像旋转360度
我需要在每次旋转完成后2秒延迟。我如何设置延迟?我已经完成了图像旋转算法。
func rotate360Degrees(_ duration: CFTimeInterval = 1.0, completionDelegate: CAAnimationDelegate? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI * 2.0)
rotateAnimation.duration = duration
if let delegate: CAAnimationDelegate = completionDelegate {
rotateAnimation.delegate = delegate
}
rotateAnimation.repeatCount = Float.greatestFiniteMagnitude;
self.layer.add(rotateAnimation, forKey: nil)
}
对于延迟我已经试过这样:
let deadlineTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
self.imgView.rotate360Degrees()
})
它无法正常工作。
什么不起作用?你没有调用'rotate360Degrees',或者该方法不会导致旋转?或旋转工作,但旋转之间没有停顿? (我最好的猜测:最后一个,正确的答案涉及'CAKeyframeAnimation'因为你正在做的是期待延迟开始动画永远在动画内部) – Tommy
旋转工作,但我需要2秒延迟在每一次旋转中该部分不工作。 – user12346