iphoneobjective-ciosrotationcatransformlayer

How to rotate a view in 3d by 360 degree?


I am developing an app in which i am rotating a view by 360 degree but i am not able to rotate it 360 degree. It is getting rotated by 180 degree and after getting rotated it is coming back to it's original position..

animation = [CABasicAnimation animationWithKeyPath:@"transform"];
transform = CATransform3DMakeRotation(3.14f, 1.0f, 1.0f, 1.0f);  
value = [NSValue valueWithCATransform3D:transform]; 
[animation setToValue:value];
[animation setAutoreverses:YES]; 
[animation setDuration:0.9];
[[rotatBall layer] addAnimation:animation forKey:@"180"];  //rotatBall is a view

In other words,it is making back and forth rotation. How to rotate it continuously.... Kindly help...


Solution

  • That's a little bit tricky but you can find it in the docs.
    To rotate it continuously you first need to set the toValue then set the animation repeat count to HUGH_VALF (defined in math.h).

        CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    
        transform = CATransform3DMakeRotation(1.0f, 1.0f, 1.0f, 1.0f);  
        fromValue = [NSValue valueWithCATransform3D:transform];
    
        transform = CATransform3DMakeRotation(M_PI*2, 1.0f, 1.0f, 1.0f);  
        toValue = [NSValue valueWithCATransform3D:transform];
    
        animation.fromValue = fromValue;
        animation.toValue = toValue;
        animation.duration = 0.9f; 
        animation.repeatCount = HUGE_VALF;     // HUGE_VALF is defined in math.h so import it
        [rotatBall.layer addAnimation:animation forKey:@"rotation"];