rotate a UIView around its center but several times

Tzur Sayag picture Tzur Sayag · Feb 6, 2009 · Viewed 31.9k times · Source

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 = CGAffineTransformMakeRotation(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 animatationBlock. 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

Answer

Jason Medeiros picture Jason Medeiros · Feb 6, 2009

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

Objective-C

UIView *viewToSpin = ...;    
CABasicAnimation* spinAnimation = [CABasicAnimation
                                  animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
[viewToSpin.layer addAnimation:spinAnimation forKey:@"spinAnimation"];

Swift 5.0

let viewToSpin = UIView() // However you have initialized your view
let spinAnimation = CABasicAnimation.init(keyPath: "transform.rotation")
spinAnimation.toValue = NSNumber(value: 5.0 * 2.0 * Float.pi)
viewToSpin.layer.add(spinAnimation, forKey: "spinAnimation")