理解 CGPathAddArc 时遇到问题
在 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 beCGPathAddArc(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
0的位置在X轴上,如下所示:
-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:
-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