1

I have done research and saw many questions regarding my issue. My issue is that, I have a CATransformLayer where I am drawing a 3D cube. Now to rotate the cube, I am using the CABasicAnimation and the code for that is as follows:

/* Code to add cube to the view */
transformLayer = [CATransformLayer layer];
transformLayer.anchorPoint = P(.5,.5);
transformLayer.bounds=CGRectMake(0,0,480,300 );
transformLayer.frame=CGRectMake(0,300, 480, 300);
transformLayer.position=P(240,150);


[transformLayer addSublayer:sideOne];
[transformLayer addSublayer:sideTwo];
[transformLayer addSublayer:sideThree];
[transformLayer addSublayer:sideFour];
[transformLayer addSublayer:sideFive];
[transformLayer addSublayer:sideSix];

transformLayer.anchorPointZ = Layer_Size/-2;


/* code to tilt the cube so that it looks 3D */    
float degreesVariance = 130;
float radiansToRotate = DegreesToRadians( degreesVariance );
transformLayer.transform=CATransform3DMakeRotation(radiansToRotate, 1, 1, 0.0);
[rootLayer addSublayer:transformLayer];

The code for animation is as follows:

/* code to rotate the cube */
CABasicAnimation *spinningAnimationX;
spinningAnimationX = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
spinningAnimationX.fromValue=0;
spinningAnimationX.toValue = [NSNumber numberWithFloat: DegreesToRadians(270) * 1* 1 /*rotations*/ * 2 ];
spinningAnimationX.duration = 2;
spinningAnimationX.repeatCount = 1.0; 
[spinningAnimationX setRemovedOnCompletion:NO];
spinningAnimationX.fillMode = kCAFillModeForwards;

/* code to add to the layer */
CAAnimationGroup *group = [CAAnimationGroup animation];
[group setDuration:2];
[group setRemovedOnCompletion:NO]; 
[group setAnimations:[NSArray arrayWithObjects: spinningAnimationX, nil]];
[transformLayer addAnimation:group forKey:nil];

Despite setting the removeOnCompletion to NO and setFillMode, the CATransformLayer that I am trying to rotate doesn't end up in the angle after animation. It snaps back to the previous original position only.

Please help me out and let me know if I am overlooking anything.

jeevangs
  • 306
  • 5
  • 20
  • possible duplicate of [Animation of stroke of circle segment ends in complete stroke](http://stackoverflow.com/questions/9142888/animation-of-stroke-of-circle-segment-ends-in-complete-stroke) – rob mayoff Mar 16 '12 at 21:03
  • It's not quite the same question, but the answer and the explanation are the same. http://stackoverflow.com/a/9143116/77567 – rob mayoff Mar 16 '12 at 21:04
  • 1
    Thanks a lot guys..! I found out the solution. Since I am grouping all the animations as CAAnimationGroup, I need to set the fillMode property to the group which I missed. – jeevangs Mar 22 '12 at 16:52

0 Answers0