0

I want to rotate image 360 degrees endlessly using iPhone SDK?

How can this be done?

Thanks!

meetpd
  • 9,150
  • 21
  • 71
  • 119
  • I need to create "wheel of fortune". I have the circle image, but I am not sure how will I rotate it. Can you please help? – meetpd May 21 '11 at 06:34
  • 1
    Do you know how to display the image? Do you know how to display it at an angle? Do you know how to setup a timer that changes the angle at regular intervals? To reiterate: which part are you stuck on? It is usually easier to get help on stackoverflow if you demonstrate that you have made some kind of effort to solve the problem on your own. – Marcelo Cantos May 21 '11 at 06:38
  • hmmm..you gave me some idea..let me try... – meetpd May 21 '11 at 06:40
  • can you tell me how to display it at an angle using timers? – meetpd May 21 '11 at 06:41

2 Answers2

4
-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
    spinWheel.userInteractionEnabled = FALSE;
    float totalRevolutions = revPerSecond * time;
    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];

    CABasicAnimation* spinAnimation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation"];
    CGAffineTransform transform = spinWheel.transform;
    float fromAngle = atan2(transform.b, transform.a);
    float toAngle = fromAngle + (totalRevolutions*4*M_PI);
    spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
    spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
    spinAnimation.repeatCount = 0;
    spinAnimation.removedOnCompletion = NO;
    spinAnimation.delegate = self;
    spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
                                    kCAMediaTimingFunctionEaseOut];
    [spinWheel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    [CATransaction commit];
}

Try using this, it works.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Mehul darji
  • 186
  • 1
  • 9
0

Assuming you are displaying the image in a UIImageView, you can rotate the view's local coordinate system (see here) and use NSTimer to increase the rotation periodically (see here).

(I would use OpenGL for this, but that's just a personal preference and is probably overkill for your situation.)

Community
  • 1
  • 1
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365