我需要无限旋转视图直到被告知停止

Adr*_*rez 1 animation rotation uiview ios swift

我看过很多类似的帖子,但没有一个有效。

到目前为止,这是我的代码。

func startRotating(view : UIImageView, rotating : Bool) {

    //Rotating the rotatingClockBarImage
    UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveLinear], animations: {

        view.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

    }, completion: {finished in

        UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveLinear], animations: {

            view.transform = CGAffineTransform(rotationAngle: 0)

        }, completion: nil)
    })//End of rotation

    continueRotating(view: view)

}
Run Code Online (Sandbox Code Playgroud)

最初的问题是我无法旋转完整的 360 度。我通过在完成时旋转一半和另一半来解决这个问题。

现在的问题是一旦这个动画完成,就是这样。我试过把它放在一个 while 循环中,for 循环,来回调用两个类似的函数。没有任何效果,它只是一直冻结我的应用程序。

例如,在 for 循环中,将运行 3 次,我放置了一个 print()。打印写入控制台三次,但动画只发生一次。正因为如此,我认为动画只是在它开始之前就被切断了,最后的旋转是唯一播放的。所以我需要找到一种方法让它播放每个旋转。

看到 Apple 的飞机在游戏应用程序中的旧版本 Xcode 中如此轻松地旋转,这不应该是那么难。我试图避免删除并重新安装旧版本,以便我可以查看该代码。

ank*_*kit 6

其实会更简单:

   extension UIView {
    func rotate360Degrees(duration: CFTimeInterval = 1.0) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = CGFloat.pi * 2
        rotateAnimation.duration = duration
        rotateAnimation.repeatCount = Float.infinity
        self.layer.add(rotateAnimation, forKey: nil)
    }

    func stopRotating(){
        self.layer.sublayers?.removeAll()
        //or
        self.layer.removeAllAnimations()
    }
}
Run Code Online (Sandbox Code Playgroud)

然后旋转: yourView.rotate360Degrees()

停止:你的视图。停止旋转()