有理解CGPathAddArc的问题

时间:2011-07-28 10:59:57

标签: iphone cocoa-touch core-animation uiviewanimation

在iPad应用程序中,我想沿中心点(768,512)和半径512的弧线逆时针移动一层。我希望它从12点开始(这是顶部屏幕右上角)并在6点钟(右下角)完成。

经过大量的尝试和失败,我得到了代码

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.

由于

利奥

1 个答案:

答案 0 :(得分: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