如何用CGPath绘制多边形?
我一直在阅读文档,但是我并不清楚如何使用 CGPath 绘制多边形。我需要做的就是围绕这样的内容绘制 CGPath:
__
\ \
\ \
\__\
任何人都可以提供有关如何执行此操作的片段吗?
另外,我假设 CGPathContainsPoint 将帮助我确定一个点是否在这样的路径内?或者该路径是否必须是实体图
另外我如何移动 cgpath?这就像在 cgrect 中改变原点一样简单吗?
谢谢。
-奥斯卡
I have been reading thru the documentation however it is not immediatly clear to me how to draw a polygon using CGPath. All I need to do is to draw CGPath around something like this:
__
\ \
\ \
\__\
Could anyone please provide an snippet on how to do this?
Additionally I assume CGPathContainsPoint will help me determine if a point is inside such path?, or does the path have to be a solid drawing
Also how can i move the cgpath around? Is this as easy as changing something like the origin just like in cgrect?
Thank you.
-Oscar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是如何使用 CGPath 创建三角形的示例,您只需放置点即可。
这是结果
This is an example of how to create a triangle using CGPath, you only have to put the points.
This is the result
你应该这样做:
注意这里,点是你想要为其绘制多边形的点的数组。所以它应该是圆形路径,如下所示:您正在绘制一个点三角形
(x1, x2, x3)
然后您应该传递到数组(x1, x2, x3, x1)
>。希望这有帮助。
You should do it like this:
Note here, the points is the array of points you want to draw the polygon for. So it should be circular path like: You are drawing a triangle of points
(x1, x2, x3)
then you should pass into array(x1, x2, x3, x1)
.Hope this helps.
Draelach 的答案已更新为 Swift 4:
Draelach's answer updated to Swift 4:
请参阅 Apple 的 QuartzDemo 应用程序。它有执行此操作的代码,以及许多其他 Quartz 绘图函数。
See Apple's QuartzDemo application. It has code for doing this, as well as many other Quartz drawing functions.
斯坦福大学在 iPhone 上的 CS193P 课程有一个名为 HelloPoly 的项目,它可能正是您想要的 - 请参阅
Stanford's CS193P class on iPhone had a project called HelloPoly that might be exactly what you want - see class home page for the spec and then see the video for how it was implemented (and google solutions from people who did the assignment).