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
主要代码:
#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
运行效果:
博客代码










