Chr*_*ris 2 math cocoa-touch ios swift
我有以下代码,可以转换 imageView 并在触摸时在其超级视图中旋转它:
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var touch: AnyObject? = event.allTouches()?.anyObject()
var location = touch!.locationInView(touch!.view)
var dx: Float = Float(location.x - hsbWheel250ImageView.center.x)
var dy: Float = Float(location.y - hsbWheel250ImageView.center.y)
var radians = atan2f(dy, dx)
//deltaAngle is set in touchesBegan
var angleDiff = deltaAngle - radians
hsbWheel250ImageView.layer.transform = CATransform3DMakeRotation(-angleDiffCGFloat, 0, 0, 1)
//This is where my problem is
var angleDiffCGFloat = CGFloat(deltaAngle - radians)
var angleInDegrees = angleDiffCGFloat * (180.0 / Float(M_PI))
println(angleInDegrees)
//For the first 180 degrees of the rotation angleInDegrees returns the correct
//number i.e. 0-180. But then after 180, where it should return 181,
//it returns -179. Where it should return 270 it returns -90 and so on
//and so forth..
Run Code Online (Sandbox Code Playgroud)
我一直在尝试用谷歌搜索我的方法,但人们给出的唯一解决方案(当从弧度转换为度数时)是上面的那个......我认为这只是一个数学问题,但我担心我的大脑有关闭此功能。
任何帮助是极大的赞赏。
atan2f返回主角,其范围为 -\xcf\x80 到 +\xcf\x80(-180 到 +180 度)。您可以显式处理负值,也可以使用fmodf强制角度进入所需的范围,例如更改:
var angleInDegrees = angleDiffCGFloat * (180.0 / Float(M_PI))\nRun Code Online (Sandbox Code Playgroud)\n\n到:
\n\nvar angleInDegrees = fmodf(360.0 + angleDiffCGFloat * (180.0 / Float(M_PI)), 360.0);\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1595 次 |
| 最近记录: |