How to rotate an image continuously

I have a wheel image in .png format, I want to know how to animate it to rotate continuously, I searched on stackoverflow and found some code fragments that helped me rotate the image, but it won’t rotate continuously, it just rotated in for a few seconds and stopped, the following code

code in viewdidload

UIImageView *imageToMove = [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"horo_circle.png"]]; [self.view addSubview:imageToMove]; [self rotateImage:imageToMove duration:5.0 curve:UIViewAnimationCurveEaseIn degrees:180]; 

and animation

 - (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration curve:(int)curve degrees:(CGFloat)degrees { // Setup the animation [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:duration]; [UIView setAnimationCurve:curve]; [UIView setAnimationBeginsFromCurrentState:YES]; // The transform matrix CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees)); image.transform = transform; // Commit the changes [UIView commitAnimations]; } 

and the following lines after import

 #define M_PI 3.14159265358979323846264338327950288 /* pi */ #define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI) 
+6
source share
5 answers

You better do this with CABasicAnimation :

 if ([self.spinnerOverlay animationForKey:@"SpinAnimation"] == nil) { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; animation.duration = 10.0f; animation.repeatCount = INFINITY; [self.spinnerOverlay.layer addAnimation:animation forKey:@"SpinAnimation"]; } 

In this code, I check if the whole animation is ready, I do not need to install it again. spinnerOverlay is in your UIImageView case that you want to rotate.

To stop the animation:

  [self.spinnerOverlay.layer removeAnimationForKey:@"SpinAnimation"]; 
+46
source

Below is the following UIImageView code called iconView until it turns == NO .

The image will always return to its original position.

 - (void)performRotationAnimated { [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ self.iconView.transform = CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished){ [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ self.iconView.transform = CGAffineTransformMakeRotation(0); } completion:^(BOOL finished){ if (_rotate) { [self performRotationAnimated]; } }]; }]; } 

This is a variation on the same topic.

(Total duration: 2 * 0.5 = 1 s for back)

It works like magic!

+3
source

Here is another way to do this with a timer. In your viewController.h file, you will need to specify your timer and image:

 @interface ViewController : UIViewController { IBOutlet UIImageView *spinningImage; NSTimer *spinTimer; } 

Then, in your ViewController.m, you add this code and you can make the spin move faster, reducing the time interval or increasing the conversion interval.

 -(void) viewDidLoad { spinTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target: self selector:@selector(spinVoid) userInfo:nil repeats:YES]; } -(void) spinVoid { spinningImage.transform = CGAffineTransformRotate(spinningImage.transform, 0.001); } 

Hope this helps!

+2
source

Now that we are on iOS6, consider switching to block animations (available with iOS 4.0) rather than the classic method. Try the following:

 [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionRepeat animations:^{ image.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees)); } completion:nil]; 
+1
source

Using the method you selected for the animation:

  [UIView setAnimationRepeatCount:10000000]; 

or you can specify the UIViewAnimationOptionRepeat flag in the animateWithDuration:delay:options:animations:completion: block method

-1
source

All Articles