问题描述
我有一个应用程序,只要用户按下屏幕,它就会为仪表上的指针设置动画.当手指抬起时,我需要知道针的旋转角度.手指一抬起,我就删除了所有动画,但我不知道如何获得针的当前旋转角度.
I have an app which animates a needle on a meter as long as the user is pressing on the screen. When the finger is lifted I need to know the rotation angle of the needle. I remove all animations as soon as the finger is lifted but I can't figure how to get the current rotation angle of the needle.
推荐答案
很简单,这是完整的解决方案:
It is quite simple, this is the full solution:
示例设置:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 6) // just to test (it is 30 in degrees and 0.523598775598299 in radians)
代码:
let rad: Double = atan2( Double(imageView.transform.b), Double(imageView.transform.a))
let deg: CGFloat = CGFloat(rad) * (CGFloat(180) / CGFloat.pi )
print(deg) // works, printing 30
其中 deg = 度数
和 rad = 弧度
说明:第一行是获取弧度,第二行是将弧度乘以弧度的等价物,得到度数.
Explanation:The first line is getting the radians, and the second line is multiplying the radians by the equivalent of a radian in degrees, to get the degrees.
注意:
在
CGAffineTransform(rotationAngle: someValue)
中,someValue
实际上是角度的弧度,不是以度数来衡量的.有关以下内容的更多信息:
In
CGAffineTransform(rotationAngle: someValue)
,someValue
is, in fact, the radians of the angle, it is not measured in degrees. More information about:
弧度值CGFloat.pi
是 180,因此您可以根据此测试任何角度.
The value in degrees of the radian CGFloat.pi
is 180, therefore you can test it for any angle depending on this.
让我知道这是否有帮助!
Let me know if this helps!
这篇关于iOS Swift 检测图像旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!