Swift Continuous Rotation Animation not so continuous

前端 未结 3 1896
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 18:59

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 si

相关标签:
3条回答
  • 2021-02-04 19:22

    Try below extension for 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, UILabel and many more.

    0 讨论(0)
  • 2021-02-04 19:27

    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()
    } 
    
    0 讨论(0)
  • 2021-02-04 19:36

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题