在IOS中将弧度转换为度数

时间:2014-05-14 07:07:25

标签: ios

嗨朋友我需要将弧度值转换为Degree。下面的代码是获得弧度

UIView *view = [self view];
   CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
   CGPoint currentTouchPoint = [touch locationInView:view];
   CGPoint previousTouchPoint = [touch previousLocationInView:view];

   CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);

这是在 touchesMoved:(NSSet *)触及withEvent:(UIEvent *)事件中完成的。但是我需要将 angleInRadians CGFloat值转换为 45.0度的程度。我怎样才能做到这一点。

我尝试了以下方法,但我找不到解决方案:

  1. #define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
  2. CGFloat RadiansToDegrees(CGFloat radians) { return radians * 180 / M_PI; };

3 个答案:

答案 0 :(得分:2)

我得到了解决方案..在我的代码中,我只获得当前移动的角度,而不是我视图的当前角度。

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

使用此代码我得到了解决方案。

感谢所有花时间陪伴我的人。

答案 1 :(得分:0)

试试这个:

 // Use the following macro in your code to convert radians to degrees:

 #define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
 NSLog(@"Output radians as degrees: %f", RADIANS_TO_DEGREES(0.584));

否则请尝试(acos(0)*180)/M_PI

注意:使用acosf(float)代替acosf(double)

答案 2 :(得分:0)

(radians) * (180.0 / M_PI)总是给予学位,计算中可能有错误尝试下面的代码可能对你有帮助..


 - (double)getTheAngleBetweenPoint:(CGPoint)point1 andPoint:(CGPoint)point2
  {
    double angle;
    CGFloat delta_y = point2.y - point1.y;
    if(delta_y < 0)
    {
       delta_y *= -1;
    }
    CGFloat delta_x = point2.x  - point1.x;
    if(delta_x < 0)
    {
      delta_x *= -1;
    }

    angle = atan2(delta_y, delta_x) * (180/M_PI) ; //returning the tan of the angle in degree

   return angle;
 }


 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch *touch = [touches anyObject];
     UIView *view = [touch view];
     double angle = [self getTheAngleBetweenPoint:[touch locationInView:view] andPoint:[touch previousLocationInView:view]];
     NSLog(@"%f",angle);

}