0

I'm trying to make a simple "rotating gear animation." Basically, I want to put a circle on the screen at a certain position, and give this circle an image of gear (or just make the image rotating without creating a circle at first). Also, the gear should keep rotating. How can I achieve that?

I'm beginner to ios development. If anyone can provide a simple sample code, I will really appreciate that!

1 Answers1

0

You have to actually rotate a UIImageView infinitely.

Firstly #import <QuartzCore/QuartzCore.h>

  - (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:    (CGFloat)rotations repeat:(float)repeat;
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

Assume that you have a UIImageView and assign an image on it and named ImgView. On viewDidLoad, add your code as below:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationRepeatCount:MAXFLOAT];
ImgView.transform = CGAffineTransformMakeRotation(M_PI);
[UIView commitAnimations];

From the links :-

UIView Infinite 360 degree rotation animation?

Infinite rotating image background UIImageView

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • wouldn't it be better to use block-based animations as the Apple recommends for iOS4+? now, we are almost iOS7... – holex Aug 03 '13 at 14:05