Search code examples
iosiphonecocoa-touchuiimageviewuikit

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?


Solution

  • 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)