As all know ,CGContextRef 与 CGMutablePathRef 都是画图工具,其中CGMutablePathRef 可与CAShapeLayer或CGContextRef配合使用.
获取当前图形上下文:
CGContextRef context =UIGraphicsGetCurrentContext();
设置线条颜色:
CGContextSetStrokeColorWithColor(context,color1.CGColor);
CGContextSetRGBStrokeColor(context,R,G,B);
设置填充颜色:
CGContextSetFillColorWithColor(context, aColor.CGColor);
绘图起点:
CGContextMoveToPoint(context,x,y);
连接线段:CGContextAddLineToPoint(context,200,20);
画弧度:CGContextAddArc(CGContextRef cg_nullable c, CGFloat x, CGFloat y,
CGFloat radius, CGFloat startAngle, CGFloat endAngle,int clockwise)
线段绘制结束:CGContextStrokePath(context);
弧度绘制结束:
CGContextClosePath(context);
CGContextDrawPath(context,kCGPathFillStroke);
CAShapeLayer *shapeLayer = [CAShapeLayerlayer];
[shapeLayer setStrokeColor:[UIColorblackColor].CGColor];
[shapeLayer setLineWidth:1];
[shapeLayer setLineJoin:kCALineJoinRound];
设置虚线的线宽及间距
[shapeLayer setLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:2], [NSNumbernumberWithInt:3],nil]];
创建虚线绘制路径
CGMutablePathRef path =CGPathCreateMutable();
设置y轴方向的虚线
CGPathMoveToPoint(path,NULL, point.x, point.y);
CGPathAddLineToPoint(path,NULL, point.x,zzHeight-margin);
设置虚线绘制路径
[shapeLayer setPath:path];
CGPathRelease(path);
[self.layeraddSublayer:shapeLayer];
添加弧度路径
void CGPathAddQuadCurveToPoint(CGMutablePathRef cg_nullable path,
const CGAffineTransform *__nullable m, CGFloat cpx, CGFloat cpy,
CGFloat x, CGFloat y)
CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
CGContextAddPath(context,path);
CGContextDrawPath(context,kCGPathStroke);
CGPathRelease(path);
[shapeLayer setPath:path];
CGPathRelease(path);
附: