我正在Swift中开发iOS应用程序,我希望用动作旋转360度的图像。动画应该在每轮完成后停止2秒钟。
每次旋转完成后我需要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()
})
它不起作用。
答案 0 :(得分:0)
非常简单。只需在一个体内调用你的函数,一旦完成旋转,在延迟2秒后调用相同的函数。什么时候你想停止只是停止删除它。实施例
unowned let unownedSelf = self
let deadlineTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
unownedSelf.functionToCall()
})