Maison > Article > base de données > Quartz2D
Quartz2D中还有以CGPath开头的API,Path我们称之为路径,我们之前用Quartz2D画出的东西都是沿着一条路径去画的。 而这次我们将来学一下如何先将路径定好之后,再将路径放入图层上下文中。 Path的使用步骤: 1.创建路径CGMutablePathRef path = CGPathCreatMut
Quartz2D中还有以CGPath开头的API,Path我们称之为路径,我们之前用Quartz2D画出的东西都是沿着一条路径去画的。
而这次我们将来学一下如何先将路径定好之后,再将路径放入图层上下文中。
Path的使用步骤:
1.创建路径CGMutablePathRef path = CGPathCreatMutable();
2.通过CGPathAddLineToPoint、CGPathAddArc、CGPathAddEllipseInRect定义路径
3.将路径添加到上下文中,CGContextAddPath
主要代码:
<code><span>#import <span>"DrawSomePath.h"</span></span> <span><span>@implementation</span> <span>DrawSomePath</span></span> -(<span>void</span>)drawRect:(<span>CGRect</span>)rect{ <span>//获得图形上下文</span> CGContextRef context = UIGraphicsGetCurrentContext(); <span>//创建一个可变的路径</span> CGMutablePathRef path = CGPathCreateMutable(); <span>//使用CGPath开头的方法创建要绘制的路径</span> CGPathAddEllipseInRect(path, <span>NULL</span>, CGRectMake(<span>10</span>, <span>10</span>, <span>100</span>, <span>100</span>)); CGPathAddEllipseInRect(path, <span>NULL</span>, CGRectMake(<span>20</span>, <span>20</span>, <span>80</span>, <span>80</span>)); CGPathAddEllipseInRect(path, <span>NULL</span>, CGRectMake(<span>30</span>, <span>30</span>, <span>60</span>, <span>60</span>)); CGPathAddEllipseInRect(path, <span>NULL</span>, CGRectMake(<span>40</span>, <span>40</span>, <span>40</span>, <span>40</span>)); CGPathAddEllipseInRect(path, <span>NULL</span>, CGRectMake(<span>50</span>, <span>50</span>, <span>20</span>, <span>20</span>)); CGContextSetLineWidth(context, <span>5</span>); <span>//将路径加入上下文中</span> CGContextAddPath(context, path); <span>//开始渲染</span> CGContextStrokePath(context); } <span>@end</span></code>
运行效果:
博客代码