I need to rotate a UIView
indefinitely. However, I need to be able to stop it per a method, as I'm trying to implement something like a custom UIActivityIndicatorView
. The view also needs to animate back to its starting rotation (0 degrees). The problem with all the approaches I've been trying to implement so far is that I'm either unable to stop it manually (until the duration ends), the animation isn't smooth or my view doesn't return back to its starting position.
Basically, what I need is an animation to rotate my view forever. Once I call a method to stop it, it should return to its starting point and stop.
I've tried a few modified answers here, but no success.
OK, just built this to check if it would work.
In Swift (because I'm learning) I've done this...
Essentially, each single rotation is one animation. Then in the completion block I inspect a boolean
rotating
. Ifrotating == true
then I run the rotation again. and again. and again.When
rotating == false
then I just don't run the animation again from the completion block.This ensures that the last animation gets to its end before actually stopping the animation.
Objective-C version
Add this code where ever you want to perform your animation
In Objective-C
To invoke/start animation
To stop animation
In Swift 2.2
To invoke/start animation
To stop animation