将弧度转换为角度

问题描述 投票:2回答:1

我有以下代码可以转换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..

我一直在尝试谷歌搜索,但人们给出的唯一解决方案(当从弧度转换为度时)是上述解决方案...我认为这只是一个数学问题,但我担心大脑已经为此关闭了。

非常感谢您的帮助。

ios cocoa-touch math swift
1个回答
3
投票
var angleInDegrees = angleDiffCGFloat * (180.0 / Float(M_PI))

至:

var angleInDegrees = fmodf(360.0 + angleDiffCGFloat * (180.0 / Float(M_PI)), 360.0);
© www.soinside.com 2019 - 2024. All rights reserved.