理解 CGPathAddArc 时遇到问题

发布于 2024-11-26 16:05:26 字数 1283 浏览 1 评论 0原文

在 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.

谢谢

Leo

In an iPad application, I want to move a layer anti-clockwise along an arc which has a center point of (768, 512) and radius of 512. I want it to start at 12 o'clock (which is top right corner of the screen) and finish at 6 o'clock (bottom right corner).

After a lot of try-and-fail, I got the code working

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"];

But the problem is I can't understand the starting angle and end angle parameter. Why should I use -M_PI_2 and M_PI_2 respectively and set the clockwise to YES?

I think I am moving the object from 90 degree to 270 degree anti-clockwise, thus the code should be
CGPathAddArc(curvedPath, NULL, 768, 512, 512, -M_PI_2, M_PI_2, YES);

I am probably wrong in multiple places and by chance got the correct result.

Please correct me and help me understand the two angle parameters:

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.

Thanks

Leo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2024-12-03 16:05:26

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

The location of 0 is on the X axis, like this:

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

-PI/2 is equivalent to 3PI/2.

You're effectively starting your rotation at the same place (-PI/2, 3*PI/2, 5*PI/2, etc., are all equal)

"12 o'clock" as you're thinking of it is 3*PI/2 or -PI/2... and you're rotating down to "6 o'clock" which is PI/2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文