iOS Swift Detecting Image Rotation

南楼画角 提交于 2019-12-08 07:21:29

问题


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.


回答1:


It is quite simple, this is the full solution:

Sample Setup:

imageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 6) // just to test (it is 30 in degrees and 0.523598775598299 in radians)

Code:

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

where deg = degrees and rad = radians

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.


NOTES:

  1. In CGAffineTransform(rotationAngle: someValue), someValue is, in fact, the radians of the angle, it is not measured in degrees. More information about:

    • radian

    • degree

    • PI

  2. 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!



来源:https://stackoverflow.com/questions/42165795/ios-swift-detecting-image-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!