ios 如何将 UIImageView 旋转 20 度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/852863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I rotate an UIImageView by 20 degrees?
提问by Thanks
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.
如果我需要旋转一个,我该UIImageView
怎么办?我有一个UIImage
我想旋转 20 度。
The Apple docs talk about a transformation matrix, but that sounds difficult. Are there any helpful methods or functions to achieve that?
Apple 文档讨论了转换矩阵,但这听起来很困难。是否有任何有用的方法或功能来实现这一目标?
回答by alexmorhun
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.
如果要向右转,该值必须大于0 如果要向左旋转,则用符号“-”表示该值。例如-20。
CGFloat degrees = 20.0f; //the value in degrees
CGFloat radians = degrees * M_PI/180;
imageView.transform = CGAffineTransformMakeRotation(radians);
Swift 4:
斯威夫特 4:
let degrees: CGFloat = 20.0 //the value in degrees
let radians: CGFloat = degrees * (.pi / 180)
imageView.transform = CGAffineTransform(rotationAngle: radians)
回答by Ed Marty
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)
(.34906585 是 20 度的弧度)
Swift 5:
斯威夫特 5:
imgView.transform = CGAffineTransform(rotationAngle: .34906585)
回答by Frank Dev
Swift version:
迅捷版:
let degrees:CGFloat = 20
myImageView.transform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI/180) )
回答by William Hu
Swift 4.0
斯威夫特 4.0
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180))
回答by JP Aquino
Here's an extension for Swift 3.
这是 Swift 3 的扩展。
extension UIImageView {
func rotate(degrees:CGFloat){
self.transform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI/180))
}
}
Usage:
用法:
myImageView.rotate(degrees: 20)
回答by Alex Bailey
This is an easier formatting example (for 20 degrees):
这是一个更简单的格式示例(20 度):
CGAffineTransform(rotationAngle: ((20.0 * CGFloat(M_PI)) / 180.0))
回答by Radix
_YourImageView.transform = CGAffineTransformMakeRotation(1.57);
where 1.57 is the radian value for 90 degree
其中 1.57 是 90 度的弧度值
回答by Marc W
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.
据我所知,使用矩阵 inUIAffineTransform
是在没有第三方框架帮助的情况下实现旋转的唯一方法。