xcode Swift 连续旋转动画不是那么连续

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28964346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:48:10  来源:igfitidea点击:

Swift Continuous Rotation Animation not so continuous

iosxcodeswiftanimation

提问by Kashif

Here is my code. Intent is to continuously rotate the UIImageView named swirls[l]. However, there is a small pause between every rotation start/end. I have gone through every single animation tutorial but cant figure out what the mistake is?

这是我的代码。意图是不断旋转名为 swirls[l] 的 UIImageView。但是,每次旋转开始/结束之间都会有一小段停顿。我已经完成了每一个动画教程,但无法弄清楚错误是什么?

let fullRotation = CGFloat(M_PI * 2)

    let duration = 2.0
    let delay = 0.0
    let options = UIViewKeyframeAnimationOptions.Repeat | UIViewKeyframeAnimationOptions.CalculationModeLinear

    UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {
        UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/3, animations: {
            swirls[l].transform = CGAffineTransformMakeRotation(1/3 * fullRotation)
        })
        UIView.addKeyframeWithRelativeStartTime(1/3, relativeDuration: 1/3, animations: {
            swirls[l].transform = CGAffineTransformMakeRotation(2/3 * fullRotation)
        })
        UIView.addKeyframeWithRelativeStartTime(2/3, relativeDuration: 1/3, animations: {
            swirls[l].transform = CGAffineTransformMakeRotation(3/3 * fullRotation)
        })

        }, completion: {finished in 
    })

EDIT: I see that it has been suggested that a previous solution is available, but it simply does not work for continuous uninterrupted rotation. The only trick that worked for me is the answer that I chose below. Thanks

编辑:我看到有人建议可以使用以前的解决方案,但它根本不适用于连续不间断的旋转。对我有用的唯一技巧是我在下面选择的答案。谢谢

回答by keval

Try below extension for swift 4.

试试下面的 swift 4 扩展。

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

For start rotation.

开始轮换。

MyView.rotate360Degrees()

And for Stop.

而对于停止。

MyView.layer.removeAllAnimations()

You can use UIButton, UILabeland many more.

您可以使用UIButtonUILabel以及更多。

回答by rdelmar

I'm not sure what's wrong with your code, but I've implemented continuous rotation using this method,

我不确定你的代码有什么问题,但我已经使用这种方法实现了连续旋转,

@IBAction func rotateView(sender: UIButton) {
    UIView.animate(withDuration: 0.5, delay: 0, options: .curveLinear, animations: { () -> Void in
        self.spinningView.transform = self.spinningView.transform.rotated(by: .pi / 2)
    }) { (finished) -> Void in
        self.rotateView(sender: sender)
    }
}

回答by Pragnesh Vitthani

If you want to continuous rotate Button or View and stop that rotation whenever you want then you can try this: For start rotation:

如果你想连续旋转按钮或视图并随时停止旋转,那么你可以试试这个:对于开始旋转:

@IBOutlet weak var RotateBtn: UIButton!
var timeTimer: Timer?

viewDidLoad()
{
  self.rotateView()
  timeTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.rotateView), userInfo: nil, repeats: true)
}

func rotateView()
{
    UIView.animate(withDuration: 0.5, delay: 0, options: .curveLinear, animations: { () -> Void in
        self.RotateBtn.transform = self.RotateBtn.transform.rotated(by: CGFloat(M_PI_4))
    })
}

If you want to stop rotation then use:

如果要停止旋转,请使用:

@IBAction func StopBtn_Pressed(_ sender: AnyObject)
{
   timeTimer?.invalidate()
   self.RecordBtn.layer.removeAllAnimations()
}