I'm trying to rotate a UIImageView
360 degrees, and have looked at several tutorials online. I could get none of them working, without the UIView
either stopping, or jumping to a new position.
- How can I achieve this?
The latest thing I've tried is:
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
But if I use 2*pi, it doesn't move at all (since it's the same position). If I try to do just pi (180 degrees), it works, but if I call the method again, it rotates backwards.
EDIT:
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
[UIView setAnimationRepeatCount:HUGE_VALF];
[UIView setAnimationBeginsFromCurrentState:YES];
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
doesn't work either. It goes to 180
degrees, pauses for a split second, then resets back to 0
degrees before it starts again.
Use quarter turn, and increase the turn incrementally.
If anyone wanted nates' solution but in swift, then here is a rough swift translation:
In Swift, you can use the following code for infinite rotation:
Swift 4
Swift 3
Stopping is like:
My contribution with a Swift Extension from the checked solution :
Swift 4.0
Deprecated :
Here is my swift solution as a UIView extension. It could be considered as a simulation of a UIActivityIndicator behaviour on any UIImageView.
This was working for me: