Understanding CABasicAnimation when spinning an object from a random angle....

Posted by user157733 on Stack Overflow See other posts from Stack Overflow or by user157733
Published on 2010-04-09T10:32:02Z Indexed on 2010/04/09 10:33 UTC
Read the original article Hit count: 453

I have spent ages trying to figure this out and I am still having problems. I want to rotate an image a random number of time - say 5 and a bit - then have it stop. I then want to rotate it again FROM ITS STOPPED POSITION. I am having difficulty with this so maybe someone can advise me on the right way to do it.

Ok so I am using a CABasicAnimation for the spin like this...

CABasicAnimation* rotationAnimation;
rotationAnimation = [NSNumber numberWithFloat: 0.0];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 1.0];
rotationAnimation.duration = 100;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 5.2;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
[myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

This works fine. I use a animationDidStop function to transform the image to the new angle so that it actually in the new position (not just appearing that way).

This is where my problems start. I have tried the following...

  1. removing the toValue line which means the animation starts from where it currently is but when the animation block is repeated it jumps back to this start position every time the block is run
  2. storing the value of the end rotation and using this in the toValue so the 2 lines of code become...
rotationAnimation = [NSNumber numberWithFloat: previousVal];
rotationAnimation.toValue = [NSNumber numberWithFloat: (M_PI * 1.0)+previousVal];

when I do this the animation still jumps because it is flicking back to the previousVal everytime the block is repeated

Therefore my final thought is to check to see if the image is at zero, if not then rotate it to zero, then impliment the block of code to spin multiple times. This seems complicated but is this the ONLY way to achieve this? I am concerned about getting the timings smooth with this method.

Any suggestions would be amazing! Thanks

© Stack Overflow or respective owner

Related posts about cabasicanimation

Related posts about iphone-sdk