Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate Rotating UIImageView

I want to rotate a UIImageView by roughly 10 degrees left/right but have a smooth animation, rather than a sudden turn which I see using:

player.transform = CGAffineTransformMakeRotation(angle)
like image 406
Josh Kahane Avatar asked Aug 27 '10 19:08

Josh Kahane


2 Answers

Converted for Swift 3/4:

let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.fromValue = 0
animation.toValue =  Double.pi * 2.0
animation.duration = 2
animation.repeatCount = .infinity
animation.isRemovedOnCompletion = false

imageView.layer.add(animation, forKey: "spin")
like image 193
Alex Avatar answered Dec 26 '22 13:12

Alex


I use some code like this to achieve a similar effect (though I rotate it by 360 degrees):

CABasicAnimation *rotate;
rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.fromValue = [NSNumber numberWithFloat:0];
rotate.toValue = [NSNumber numberWithFloat:deg2rad(10)];
rotate.duration = 0.25;
rotate.repeatCount = 1;
[self.view.layer addAnimation:rotate forKey:@"10"];

I use code very similar to this to spin an image view.

like image 37
jer Avatar answered Dec 26 '22 13:12

jer