gpt4 book ai didi

ios - 我需要无限旋转 View 直到被告知停止

转载 作者:搜寻专家 更新时间:2023-11-01 06:34:07 26 4
gpt4 key购买 nike

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

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

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)

}

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

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

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

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

最佳答案

其实更简单:

   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()
}
}

然后旋转:yourView.rotate360Degrees()

停止:你的观点。停止旋转()

关于ios - 我需要无限旋转 View 直到被告知停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com