Search code examples
iphoneobjective-ccocoa-touchios4iphone-sdk-3.0

How to rotate image 360 degrees endlessly using iPhone SDK?


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

How can this be done?

Thanks!


Solution

  • -(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.