2014-05-14 67 views
1

您好朋友,我需要將弧度值轉換爲度數。下面的代碼是讓弧度將弧度轉換爲IOS中的度數

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 deg程度。我怎樣才能做到這一點。

我曾嘗試以下方法,但我不能找到解決辦法:

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

#define RADIANS_TO_DEGREES(弧度)((弧度)*(180.0/M_PI))的作品,只是試過了 – AndyOS

回答

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)

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); 

} 


2

我得到了解決方案..在我的代碼中,我只能獲取角度的角度不要爲我的觀點移動當前角度。

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

使用此代碼我得到了我的解決方案。

感謝所有花時間陪伴我的人。