Quartz2D-根据路径绘制

来源:互联网 发布:检查java是否安装成功 编辑:程序博客网 时间:2024/06/01 09:58

Quartz2D中还有以CGPath开头的API,Path我们称之为路径,我们之前用Quartz2D画出的东西都是沿着一条路径去画的。
而这次我们将来学一下如何先将路径定好之后,再将路径放入图层上下文中。

Path的使用步骤:
1.创建路径CGMutablePathRef path = CGPathCreatMutable();
2.通过CGPathAddLineToPoint、CGPathAddArc、CGPathAddEllipseInRect定义路径
3.将路径添加到上下文中,CGContextAddPath

主要代码:

#import "DrawSomePath.h"@implementation DrawSomePath-(void)drawRect:(CGRect)rect{    //获得图形上下文    CGContextRef context = UIGraphicsGetCurrentContext();    //创建一个可变的路径    CGMutablePathRef path = CGPathCreateMutable();    //使用CGPath开头的方法创建要绘制的路径    CGPathAddEllipseInRect(path, NULL, CGRectMake(10, 10, 100, 100));    CGPathAddEllipseInRect(path, NULL, CGRectMake(20, 20, 80, 80));    CGPathAddEllipseInRect(path, NULL, CGRectMake(30, 30, 60, 60));    CGPathAddEllipseInRect(path, NULL, CGRectMake(40, 40, 40, 40));    CGPathAddEllipseInRect(path, NULL, CGRectMake(50, 50, 20, 20));    CGContextSetLineWidth(context, 5);    //将路径加入上下文中    CGContextAddPath(context, path);    //开始渲染    CGContextStrokePath(context);}@end

运行效果:
【图1】
博客代码

0 0