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条回答
时光不老,我们不散
2楼-- · 2020-01-30 06:14

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)
查看更多
叛逆
3楼-- · 2020-01-30 06:17

Swift version:

let degrees:CGFloat = 20
myImageView.transform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI/180) )
查看更多
劫难
4楼-- · 2020-01-30 06:18

Swift 4.0

imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180))
查看更多
淡お忘
5楼-- · 2020-01-30 06:27

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.

查看更多
欢心
6楼-- · 2020-01-30 06:30

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)
查看更多
Viruses.
7楼-- · 2020-01-30 06:30
_YourImageView.transform = CGAffineTransformMakeRotation(1.57);

where 1.57 is the radian value for 90 degree

查看更多
登录 后发表回答