问题
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:
In
CGAffineTransform(rotationAngle: someValue),someValueis, in fact, the radians of the angle, it is not measured in degrees. More information about:radian
degree
PI
The value in degrees of the radian
CGFloat.piis 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