Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to spin an image continuously

I have an wheel image in .png format, I want to know how can i animate so that it rotates continuously, I searched on stackoverflow and found certain snippets of code which helped me rotate my image but it wouldn't rotate continuously, it would just rotate for a few seconds and stop, the code as follows

the 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 the 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 the import

#define M_PI   3.14159265358979323846264338327950288   /* pi */

#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)
like image 483
BoredToDeath Avatar asked Nov 08 '12 14:11

BoredToDeath


2 Answers

You are better of doing this with a 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 whether the animation is all ready set, not need to set it again. The spinnerOverlay is in your case the UIImageView you want to rotate.

To stop the animation:

  [self.spinnerOverlay.layer removeAnimationForKey:@"SpinAnimation"];
like image 136
rckoenes Avatar answered Oct 05 '22 01:10

rckoenes


The code bellow will spin a UIImageView called iconView continiously until rotate == 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 theme.

(Total duration: 2 * 0.5 = 1 sec for a spin)

Works like magic!

like image 44
Vulkan Avatar answered Oct 04 '22 23:10

Vulkan