-2

I will answer my own question to share my solution for this piece of code.

How can i rotate any view continuously with a constant speed in IOS Swift 3?

Numan Karaaslan
  • 1,365
  • 1
  • 17
  • 25

1 Answers1

-1

Here is a simple class to rotate a view continuously at a constant speed. In this example, 1 complete tour in 1 second.

class Rotator
{
    private static var some_view : UIView = UIView()
    private static var some_timer : Timer = Timer()

    class func Rotate(some_view : UIView)
    {
        self.some_view = some_view
        some_timer.invalidate()
        some_timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(rotateView), userInfo: nil, repeats: true)
    }

    @objc private class func rotateView()
    {
        UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: { () -> Void in
            some_view.transform = some_view.transform.rotated(by: CGFloat(Double.pi))
        })
    }
}

And the usage is:

myImage = UIImageView()
Rotator.Rotate(some_view: myImage)

Hope this helps.

Numan Karaaslan
  • 1,365
  • 1
  • 17
  • 25
  • Use of class statics is silly, and this question has been asked and answered many times before so no need for a new question; if you think you have a better answer, answer one of the existing questions. – matt Feb 08 '18 at 16:27