points -> a shape:多个点构成一个形状
shapes -> a path:多个形状构成一个路径
绘制过程:
创建路径 -CGMutablePathRef CGPathCreateMutable (void); //CGMutablePathRef path = CGPathCreateMutable();
画线条,并将其加入路径
设置起点 -void CGPathMoveToPoint (CGMutablePathRef path,const CGAffineTransform *m,CGFloat x,CGFloat y); //第二个参数:NULL
设置终点 -void CGPathAddLineToPoint
(CGMutablePathRef path,const CGAffineTransform *m,CGFloat x,CGFloat y);
创建上下文环境 -CGContextRef UIGraphicsGetCurrentContext (void); //CGContextRef currentContext = UIGraphicsGetCurrentContext();
在环境中添加路径 -void CGContextAddPath (CGContextRef context,CGPathRef path);设置相关属性 - (void)setStroke //颜色
- void CGContextSetLineWidth (CGContextRef c,CGFloat width); //线宽
绘制路径 -void CGContextDrawPath (CGContextRef c,CGPathDrawingMode mode);
释放 -void CGPathRelease (CGPathRef path);
重要的3种绘制模式 -CGPathDrawingMode :kCGPathFill, kCGPathStroke, kCGPathFillStroke ;
- (void)drawRect:(CGRect)rect{
/* Create the path */
CGMutablePathRef path = CGPathCreateMutable();
CGRect screenBounds = [[UIScreen mainScreen] bounds];
/* Start:左上 */
CGPathMoveToPoint(path, NULL, screenBounds.origin.x,screenBounds.origin.y);
/* Draw a line : 从左上到右下 */
CGPathAddLineToPoint(path,NULL,screenBounds.size.width,screenBounds.size.height);
/* Start another line:右上 */
CGPathMoveToPoint(path,NULL,screenBounds.size.width,screenBounds.origin.y);
/* Draw a line :从右上到左下 */
CGPathAddLineToPoint(path,NULL,screenBounds.origin.x,screenBounds.size.height);
/* Get the context that the path has to be drawn on */
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Add the path*/
CGContextAddPath(currentContext,path);
/* Set the blue color as the stroke color */
[[UIColor blueColor] setStroke];
/* Draw the path with stroke color */
CGContextDrawPath(currentContext,kCGPathStroke);
/* Finally release the path object */
CGPathRelease(path);
}