# 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?

• __attribute__ ((deprecated)) does not work on objective-c protocol methods?
• xcode 7.1 swift framework app builds but not archiving
• Managing two NSURLConnection
• Is it possible to detect that your iOS app is running on an iPad mini at runtime?
• How to identify EKEvent uniquely with Sync across the devices
• Xcode: Is there a location/flag to prevent a Class from compiling?
• ### 7 Solutions Collect From Internet About “How can I rotate an UIImageView by 20 degrees?”

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)

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;
``````

Swift version:

``````let degrees:CGFloat = 20
myImageView.transform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI/180) )
``````
```_YourImageView.transform = CGAffineTransformMakeRotation(1.57);
```

where 1.57 is the radian value for 90 degree

This is an easier formatting example (for 20 degrees):

``````CGAffineTransform(rotationAngle: ((20.0 * CGFloat(M_PI)) / 180.0))
``````

Here’s an extension for Swift 3.

``````extension UIImageView {

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

Usage:

``````myImageView.rotate(degrees: 20)
``````

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