tags:

views:

7779

answers:

4

I'm trying to rotate some UIView around its center, so the simple code goes something like (in pseudocode):

[UIView beginAnimations:@"crazyRotate" context:nil];
[UIView setAnimationDuration:1.0];
someview.transform = CGAffineTransformMakeRotate(angle);
[UIView commitAnimations]

now if I set angle to say M_PI/2 the thing rotates nicely. if I set it to 2*M_PI, well it does "nothing". I can understand that the matrix translates to something that does nothing (rotating 360 means "stay" in a sense), yet, I want to rotate it 5 times (think of a newspaper rotate scale coming at you effect -- I'm not great at describing..hope someone understands). So, I tried adding setting angle to 180 deg (M_PI) and add a nested animatation block. but I guess that since I'm setting the same property (someview.transition) again it ignores it somehow). I tried setting repeat count of the animation to 2 with angle M_PI but it seems to simply rotate 180, going back to straight position and then initiating the rotate again.

So, I'm a little out of ideas, any help appreciated! --t

+6  A: 

You can use the following animation of your UIView's layer property. I've tested it.

UIView *viewToSpin = ...;    
CABasicAnimation* spinAnimation = [CABasicAnimation
                                  animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
[viewToSpin.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
Jason Medeiros
How can I slow down this animation?
mahboudz
For animation timing considerations, please see the reference material for the CAMediaTiming Protocol, which CABasicAnimation implements. Particularly, you'd probably want to set the 'duration' property. The code above is using the default duration of 0.25 seconds.
Jason Medeiros
A: 

Hey!

Thanks for the quick reply. This is a good solution in a sense, though 5.2*M_PI is not exactly a full spin per say. Pragmatically (are we ever though??) your solution solves the problem of course but I stil can't stop wondering why the nested animation block (each block doing a half turn) does not work. Do you have an idea?

thanx again! -t

The sample code animates 5 full rotations, not 5.2 (i.e. 5*2*M_PI, not 5.2*M_PI).
Jason Medeiros
A: 

Getting a continuous spinning effect is a little tricky, but I describe a means to do it here. Yes, Core Animation seems to optimize transforms to the closest ending position within the unit circle. The method I describe there chains a few half-rotation animations together to make full rotations, although you do notice a slight stutter in the handoff from one animation to the next.

Perhaps a CAKeyframeAnimation constructed with these half-rotation values would be the right way to go. Then you could also control acceleration and deceleration.

Brad Larson
A: 

Hey Brad, Nice, but to be honest its again a workaround and hence the little (almost un-noticed jump between the animations), If anyone has even more in depth understanding of how stacking of animations on UIView works I would love to hear about it, Again, thanks for the tips and help, --tzurs

Tzur