How can I rotate UIImageView by 20 degrees?

What should I do if I need to rotate a UIImageView ? I have a UIImage that I want to rotate 20 degrees.

Apple docs talk about conversion matrix, but it sounds complicated. Are there any useful methods or functions to achieve this?

+43
ios iphone cocoa-touch uikit uiimageview
May 12 '09 at 1:43 p.m.
source share
8 answers

The transformation matrix is ​​not incredibly complex. It is quite simple if you use the supplied functions:

 imgView.transform = CGAffineTransformMakeRotation(.34906585); 

(34906585 - 20 degrees in radians)

+89
May 12 '09 at 1:47 pm
source share

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

 CGFloat degrees = 20.0f; //the value in degrees CGFloat radians = degrees * M_PI/180; imageView.transform = CGAffineTransformMakeRotation(radians); 
+99
Mar 15 2018-12-15T00:
source share

Quick version:

 let degrees:CGFloat = 20 myImageView.transform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI/180) ) 
+10
Nov 28 '15 at 9:22
source share

Swift 4.0

 imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180)) 
+2
Oct 31 '17 at 9:09 on
source share
 _YourImageView.transform = CGAffineTransformMakeRotation (1.57);

where 1.57 is the radian value for 90 degrees

+1
Jul 04 2018-11-11T00:
source share

This is a simpler formatting example (for 20 degrees):

 CGAffineTransform(rotationAngle: ((20.0 * CGFloat(M_PI)) / 180.0)) 
+1
Jan 20 '17 at 16:19
source share

Here is the extension for Swift 3.

 extension UIImageView { func rotate(degrees:CGFloat){ self.transform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI/180)) } } 

Using:

 myImageView.rotate(degrees: 20) 
0
Jul 25 '17 at 15:38
source share

As far as I know, using a matrix in UIAffineTransform is the only way to achieve rotation without the help of a third-party structure.

-one
May 12 '09 at 1:46 pm
source share



All Articles