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{
CGMutablePathRef path = CGPathCreateMutable();
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGPathMoveToPoint(path, NULL, screenBounds.origin.x,screenBounds.origin.y);
CGPathAddLineToPoint(path,NULL,screenBounds.size.width,screenBounds.size.height);
CGPathMoveToPoint(path,NULL,screenBounds.size.width,screenBounds.origin.y);
CGPathAddLineToPoint(path,NULL,screenBounds.origin.x,screenBounds.size.height);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextAddPath(currentContext,path);
[[UIColor blueColor] setStroke];
CGContextDrawPath(currentContext,kCGPathStroke);
CGPathRelease(path);
}