2011-07-28 30 views
11

在iPad應用,我想一個層逆時針方向沿着具有的(768,512)的中心點和半徑512我希望它在12點開始的圓弧移動(這是屏幕的右上角),並在6點(右下角)結束。有問題的理解CGPathAddArc

經過大量的嘗試,和失敗,我得到的代碼工作

CGPoint origin = logo.layer.position; 

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.calculationMode = kCAAnimationPaced; 
pathAnimation.fillMode = kCAFillModeForwards; 
pathAnimation.removedOnCompletion = YES; 

CGMutablePathRef curvedPath = CGPathCreateMutable(); 
CGPathMoveToPoint(curvedPath, NULL, origin.x, origin.y); 
CGPathAddArc(curvedPath, NULL, 768, 512, 512, -M_PI_2, M_PI_2, YES); 
pathAnimation.path = curvedPath; 
CGPathRelease(curvedPath); 
pathAnimation.duration = 2; 
[logo.layer addAnimation:pathAnimation forKey:@"curve"]; 

但問題是我無法理解的起始角度和終止角度參數。爲什麼我應該分別使用-M_PI_2和M_PI_2並將順時針設置爲YES?

我想我從90度移動的物體逆時針270度,這樣的代碼應該是
CGPathAddArc(curvedPath, NULL, 768, 512, 512, -M_PI_2, M_PI_2, YES);

我在多個地方可能是錯誤的和偶然的機會得到了正確的結果。

請指正和幫助我瞭解這兩個角度參數:

由startAngle

The angle (in radians) from the horizontal that determines the starting point of the arc. 

endAngle

The angle (in radians) from the horizontal that determines the ending point of the arc. 

感謝

利奧

回答

18

0的位置在X軸上,如下所示:

3*PI/2 
     | 
PI ---|--- 0 
     | 
    PI/2 

-PI/2相當於3PI/2。

你有效地在同一個地方開始你的旋轉(-PI/2,3 * PI/2,5 * PI/2,等等,都是平等的)

「12點鐘」如你想的是3 * PI/2或-PI/2 ...和你旋轉到「6點」,這是PI/2

+1

的倒掛Y軸混淆了我。 。另外,謝謝,它幫助我:) – codrut