How can I rotate an UIImageView by 20 degrees?

2020-01-30 05:45发布

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?

8条回答
▲ chillily
2楼-- · 2020-01-30 06:32

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)
查看更多
forever°为你锁心
3楼-- · 2020-01-30 06:36

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

CGAffineTransform(rotationAngle: ((20.0 * CGFloat(M_PI)) / 180.0))
查看更多
登录 后发表回答