如何从顶部开始画一个圆
我正在使用一些使用 CGPaths 绘制圆弧的示例代码。我环顾四周并找到了文档,但我似乎无法在脑海中想象使用 MoveToPoint、AddLineToPoint 等方面发生的情况。我无法“看到”代码在做什么,我只能看到结果。
例如,下面的代码从 3 点钟位置开始绘制一个完整 360 度的圆弧。对于我的一生,我无法弄清楚如何让它从 12 点钟位置开始,并实际旋转视图 - 90 度。
有人可以帮我弄清楚这段代码以及如何更改它以实现从 12 点开始,最好尝试解释整个路径的工作原理。或者也许可以向我指出在线视觉资源?
- (void)drawPathWithArc:(CGFloat)arc {
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, 100.f, 100.f);
CGPathAddLineToPoint(thePath, NULL, 200.f, 100.f);
CGPathAddArc(thePath, NULL, 100.f, 100.f, 100.f, 0.f, (360* M_PI)/180, NO);
CGPathCloseSubpath(thePath);
shapeLayer_.path = thePath;
CGPathRelease(thePath);
}
I'm working with some example code that draws an arc using CGPaths. I've had a look around and have found documentation but I just can't seem to visualize in my head whats going on in terms of using MoveToPoint, AddLineToPoint etc. I can't 'see' what the code is doing I can just see the result.
For example the code below draws an arc a full 360 degrees beginning from the 3 o clock position. For the life of me I can't figure out how to make it begin from the 12 o clock position with actually rotating the view - 90 degrees.
Could somebody help me figure out this code and how I would change it to achieve the beginning from 12 o'clock, preferably trying to explain how this whole paths things works. Or maybe point me to a visual resource online?
- (void)drawPathWithArc:(CGFloat)arc {
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath, NULL, 100.f, 100.f);
CGPathAddLineToPoint(thePath, NULL, 200.f, 100.f);
CGPathAddArc(thePath, NULL, 100.f, 100.f, 100.f, 0.f, (360* M_PI)/180, NO);
CGPathCloseSubpath(thePath);
shapeLayer_.path = thePath;
CGPathRelease(thePath);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
路径实际上并不难在视觉上理解。基本上,所有路径都是连接定义 iPhone 屏幕的笛卡尔平面上两点的线。
当您 moveToPoint 时,它将路径的当前点设置为指定点。
当您 addLineToPoint 时,它会从当前点到指定点绘制一条直线。
当您 addCurveToPoint 时,它会根据某些切线和控制点绘制从当前点到指定点的曲线。
等等。我建议阅读苹果关于 CGPaths 的文档,以便更好地理解每个函数的作用。
http://developer.apple.com/ library/mac/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html
至于您的问题是从 12 而不是 3 开始,只需阅读 CGPathAddArc 函数的文档即可。
您需要做的是将当前代码更改
为:
将起始角度更改为 -90 度(所有角度均以水平面的弧度为单位进行测量),将结束角度更改为 270 度。
希望这有帮助。
干杯,
布伦顿。
Paths really are not that hard to understand visually. Basically all a path is is a line connecting two points on the cartesian plane that defines the iphone's screen.
When you moveToPoint it sets the current point of the path to the specified point.
When you addLineToPoint it draws a straight line from the current point to the specified point.
When you addCurveToPoint it draws a curved line from the current point to the specified point based on certain tangents and control points.
And so on. I would recommend reading apples documentation on CGPaths in order to better understand what each function is doing.
http://developer.apple.com/library/mac/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html
As far as your question goes to making this start at 12 instead of 3 simply read the documentation for the CGPathAddArc function.
What you need to do is change your current code:
to:
All this is doing is changing the starting angle to -90 degrees (all angles are measured in radians from the horizontal) and the ending angle to 270 degrees.
Hope this helps.
Cheers,
Brenton.