Understanding CABasicAnimation when spinning an object from a random angle....
- by user157733
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...
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
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