Start/stop image view rotation animations

Viewed 6109

I have a start/stop button and an image view which I want to rotate.

When I press the button I want the to start rotating and when I press the button again the image should stop rotating. I am currently using an UIView animation, but I haven't figured out a way to stop the view animations.

I want the image to rotate, but when the animation stops the image shouldn't go back to the starting position, but instead continue the animation.

var isTapped = true

    @IBAction func startStopButtonTapped(_ sender: Any) {
       ruotate()
       isTapped = !isTapped
    }

    func ruotate() {
        if isTapped {
            UIView.animate(withDuration: 5, delay: 0, options: .repeat, animations: { () -> Void in
            self.imageWood.transform =     self.imageWood.transform.rotated(by: CGFloat(M_PI_2))
            }, completion: { finished in
            ruotate()
            })
    }  }

It is my code, but it doesn't work like I aspect.

5 Answers
Related