Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees?

I'm writing an iPhone app, and I've got an image which I'ld like to have swirl outwards.

Currently my code looks like this (wrapped in a beginAnimations/commitAnimations block):

scale = CGAffineTransformScale(CGAffineTransformIdentity, 5.0f, 5.0f); swirl = CGAffineTransformRotate(scale, M_PI); [player setTransform:swirl];     [player setAlpha:0.0f]; 

But I find that if I try to change the angle of the rotation to, say, 4*M_PI, it doesn't rotate at all. Is it possible to get a 720˚ rotation using CGAffineTransformRotate, or do I have to switch to another technology?

If I have to switch to another technology, would you recommend using another thread (or a timer) to do the animation myself, or would OpenGL be a better route to go?

Thanks,
Blake.

like image 424
bwinton Avatar asked Feb 12 '09 18:02

bwinton


2 Answers

You can rotate a view, by some number of radians, regardless of whether it is less than a full rotation or many multiples of a full rotation, without having to split the rotation into pieces. As an example, the following code will spin a view, once per second, for a specified number of seconds. You can easily modify it to spin a view by a certain number of rotations, or by some number of radians.

- (void) runSpinAnimationWithDuration:(CGFloat) duration; {     CABasicAnimation* rotationAnimation;     rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];     rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];     rotationAnimation.duration = duration;     rotationAnimation.cumulative = YES;     rotationAnimation.repeatCount = 1.0;      rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];      [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } 
like image 74
mahboudz Avatar answered Sep 22 '22 14:09

mahboudz


You can, but you will need to split your animation into half-circle rotations. I provide an example of this in response to this question, using a repeating CABasicAnimation applied to the layer underneath the view. As I suggest there, doing these half-rotations as parts of a CAKeyframeAnimation would probably be the better way to structure this, because the animation would be smoother (there's a slight hitch in between half-rotations in my example), and you could do a nice acceleration / deceleration at the start and end.

like image 22
Brad Larson Avatar answered Sep 26 '22 14:09

Brad Larson