Ios 如何将UIImageView旋转20度?

Ios 如何将UIImageView旋转20度?,ios,iphone,cocoa-touch,uiimageview,uikit,Ios,Iphone,Cocoa Touch,Uiimageview,Uikit,如果需要旋转UIImageView,我必须做什么?我有一个ui图像,我想旋转20度 苹果的文档谈到了转换矩阵,但这听起来很难。有什么有用的方法或函数可以实现这一点吗?据我所知,使用UIAffineTransform中的矩阵是在没有第三方框架帮助的情况下实现旋转的唯一方法。转换矩阵并不难。如果您使用提供的函数,这非常简单: imgView.transform = CGAffineTransformMakeRotation(.34906585); (.34906585的弧度为20度) Swift

如果需要旋转
UIImageView
,我必须做什么?我有一个
ui图像
,我想旋转20度


苹果的文档谈到了转换矩阵,但这听起来很难。有什么有用的方法或函数可以实现这一点吗?

据我所知,使用UIAffineTransform中的矩阵是在没有第三方框架帮助的情况下实现旋转的唯一方法。

转换矩阵并不难。如果您使用提供的函数,这非常简单:

imgView.transform = CGAffineTransformMakeRotation(.34906585);
(.34906585的弧度为20度)


Swift 5:

imgView.transform = CGAffineTransform(rotationAngle: .34906585)
_YourImageView.transform=CGAffineTransformMakeRotation(1.57);
其中1.57是90度的弧度值

如果要向右旋转,则该值必须大于0如果要向左旋转,则表示该值带有符号“-”。例如-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)

Swift版本:

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

这是一个更简单的格式化示例(适用于20度):


这是Swift 3的分机

extension UIImageView {

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

myImageView.rotate(degrees: 20)

Swift 4.0

imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180))

谢谢我真的认为这很复杂,因为“变换矩阵”;)这应该勾选,这是迄今为止最好的答案!学位转换是一个非常好的加,这应该是最好的一个!绝对是最好的!同样的代码(let除外)也适用于objective-c。
myImageView.rotate(degrees: 20)
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180))