2012-06-02 52 views
0

我在最好的时候被弧度困惑,所以我倾向于在旋转UIViews时以度数工作。到目前为止,我一直在使用以下公式将度数转换为弧度:通过360度旋转UIView和更多

#define radians(x) (M_PI * x/180.0) 

到目前为止这么好。问题出现在用户旋转360度或更多的屏幕上的UIView,我想在屏幕上显示图像旋转了多少度。我有这样的,它工作正常可达180度:

float rot = [recognizer rotation]; 
[self.steeringWheel setTransform:CGAffineTransformRotate([self.steeringWheel transform], (rot))]; // turn wheel 

CGFloat radians = atan2f(steeringWheel.transform.b, steeringWheel.transform.a); 
CGFloat degrees = radians * (180/M_PI); 

self.degreesBox.text = [NSString stringWithFormat:@"%1.0f", degrees]; // show degrees of rotation on screen 

180度后,我读出的变化-179,-178等一路回零。相反,我希望它继续计数到359(如果可能,然后回到零,1,2等)。

我可以使用一个公式,它增加了2到179,3到178等来获得正确的数量,但是当我然后转动并且在相反方向转动轮子时,这将不起作用(-1度转动将读取出359,当我真的想要读出1或-1)。

我希望这是有道理的。基本上,我想知道轮子从起点开始在每个方向转过多少。目前我得到的是通过最短路线读取多少度数回到起点。

+0

范围为[-π,π] – DGund

回答

1

试试这个代码:

CGFloat radians = atan2f(steeringWheel.transform.b, steeringWheel.transform.a); 
if (radians < 0.0) radians += 2 * M_PI; 
CGFloat degrees = radians * (180/M_PI); 

编辑:

重读你的问题后,我看到您的问题确实是。 atan2将始终返回范围(-π,π)的结果。

看起来您希望方向盘能够向左旋转一整圈并向右旋转一整圈,您可以通过将新角度与旧角度进行比较,以便您知道用户是否正在以顺时针或逆时针方向旋转车轮

您还可以设置一个标志,当用户从开始(闲置)位置左转(CCW)相应地签署

0

夫特3
动画具有嵌套闭合比更好nimation延迟块。

UIView.animate(withDuration: 0.5, animations: { 
       button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi)) 
    }) { (isAnimationComplete) in 

      // Nested Block 
      UIView.animate(withDuration: 0.5) { 
       button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2)) 
      }  
    } 

动画与延迟和选项:

// Rotation from 0 to 360 degree  
UIView.animate(withDuration:0.5, animations: {() -> Void in 
     button.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi)) 
}) 

// Rotation from 180 to 360 degree 
UIView.animate(withDuration: 0.5, delay: 0.45, options: .curveEaseIn, animations: {() -> Void in 
     button.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * 2)) 
}, completion: nil)