Can anyone tell me how to rotate an image in circular motion
5 Answers
You can rotate a view, by some number of radians, regardless of whether it is less than a full rotation or many multiples of a full rotation, without having to split the rotation into pieces. As an example, the following code will spin a view, once per second, for a specified number of seconds. You can easily modify it to spin a view by a certain number of rotations, or by some number of radians.
- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
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 = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- 39,196
- 16
- 97
- 124
-
i'm getting an error, " rotations undeclared" – abhinav Sep 15 '09 at 06:17
-
3rotations is a const. Replace it with the number of times you want the view to rotate in duration amount of time. – mahboudz Sep 17 '09 at 11:31
-
I'm I'm not the – Kevin ABRIOUX Jan 11 '20 at 10:05
based on mahboudz's answer, you can set it to infinitely spin by setting:
rotationAnimation.repeatCount = HUGE_VALF;

- 31
- 2
If you mean, "How do you make an image follow a circular path?", then the pseudocode would be:
image.x = circleCentre.x + cos(angle) * circleRadius;
image.y = circleCentre.y + sin(angle) * circleRadius;
angle += 5;

- 3,659
- 3
- 36
- 49
This question asks the same thing, although in different words. The answers in that question, as well as in this question should provide a way to animate a UIView (UIImageView is simply a subclass of this) about its center for 360 degrees or more.

- 1
- 1

- 170,088
- 45
- 397
- 571
-
Probably u r confused about rotation and revolution. The above question is about revolution. – Biranchi Apr 30 '10 at 05:28
Creating the animation
- (CABasicAnimation *)spinAnimationWithDuration:(CGFloat)duration clockwise:(BOOL)clockwise repeat:(BOOL)repeats
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
anim.toValue = clockwise ? @(M_PI * 2.0) : @(M_PI * -2.0);
anim.duration = duration;
anim.cumulative = YES;
anim.repeatCount = repeats ? CGFLOAT_MAX : 0;
return anim;
}
Add it to a view
CABasicAnimation *animation = [self spinAnimationWithDuration:1.0 clockwise:YES repeat:YES];
[self.spinningView.layer addAnimation:animation forKey:@"rotationAnimation"];
How is this answer different from @mahboudz's answer? You will have way cleaner code if most of your functions returns objects instead of just manipulating some objects here and there.

- 22,616
- 10
- 116
- 130