Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rotate an UIImageView by 20 degrees?

What do I have to do, if I need to rotate a UIImageView? I have a UIImage which I want to rotate by 20 degrees.

The Apple docs talk about a transformation matrix, but that sounds difficult. Are there any helpful methods or functions to achieve that?

like image 681
Thanks Avatar asked May 12 '09 13:05

Thanks


2 Answers

If you want to turn right, the value must be greater than 0 if you want to rotate to the left indicates the value with the sign "-". For example -20.

CGFloat degrees = 20.0f; //the value in degrees CGFloat radians = degrees * M_PI/180; imageView.transform = CGAffineTransformMakeRotation(radians); 

Swift 4:

let degrees: CGFloat = 20.0 //the value in degrees let radians: CGFloat = degrees * (.pi / 180) imageView.transform = CGAffineTransform(rotationAngle: radians) 
like image 56
alexmorhun Avatar answered Oct 06 '22 09:10

alexmorhun


A transformation matrix is not incredibly difficult. It's quite simple, if you use the supplied functions:

imgView.transform = CGAffineTransformMakeRotation(.34906585); 

(.34906585 is 20 degrees in radians)


Swift 5:

imgView.transform = CGAffineTransform(rotationAngle: .34906585) 
like image 21
Ed Marty Avatar answered Oct 06 '22 08:10

Ed Marty