我正在用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()
    })

它不起作用。

最佳答案

很简单。只需在实体中调用函数,旋转完成后,在延迟2秒后调用相同的函数。当你想停下来的时候就停下来把它拿走。例子

unowned let unownedSelf = self

let deadlineTime = DispatchTime.now() + .seconds(2)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
 unownedSelf.functionToCall()
 })

10-06 06:37