【发布时间】:2009-05-12 13:43:26
【问题描述】:
如果我需要旋转UIImageView,我该怎么办?我有一个UIImage,我想将其旋转 20 度。
Apple 文档谈到了转换矩阵,但这听起来很困难。有什么有用的方法或功能可以实现吗?
【问题讨论】:
标签: ios iphone cocoa-touch uiimageview uikit
如果我需要旋转UIImageView,我该怎么办?我有一个UIImage,我想将其旋转 20 度。
Apple 文档谈到了转换矩阵,但这听起来很困难。有什么有用的方法或功能可以实现吗?
【问题讨论】:
标签: ios iphone cocoa-touch uiimageview uikit
如果要向右转,该值必须大于0 如果要向左旋转表示该值带有符号“-”。例如 -20。
CGFloat degrees = 20.0f; //the value in degrees
CGFloat radians = degrees * M_PI/180;
imageView.transform = CGAffineTransformMakeRotation(radians);
斯威夫特 4:
let degrees: CGFloat = 20.0 //the value in degrees
let radians: CGFloat = degrees * (.pi / 180)
imageView.transform = CGAffineTransform(rotationAngle: radians)
【讨论】:
转换矩阵并不难。如果您使用提供的功能,这很简单:
imgView.transform = CGAffineTransformMakeRotation(.34906585);
(.34906585 的弧度为 20 度)
斯威夫特 5:
imgView.transform = CGAffineTransform(rotationAngle: .34906585)
【讨论】:
Swift 版本:
let degrees:CGFloat = 20
myImageView.transform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI/180) )
【讨论】:
Swift 4.0
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180))
【讨论】:
这是 Swift 3 的扩展。
extension UIImageView {
func rotate(degrees:CGFloat){
self.transform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI/180))
}
}
用法:
myImageView.rotate(degrees: 20)
【讨论】:
其中 1.57 是 90 度的弧度值
【讨论】:
这是一个更简单的格式化示例(20 度):
CGAffineTransform(rotationAngle: ((20.0 * CGFloat(M_PI)) / 180.0))
【讨论】:
据我所知,使用UIAffineTransform 中的矩阵是在没有第三方框架帮助的情况下实现旋转的唯一方法。
【讨论】: