CGContextRef 与CGMutablePathRef画图用法

来源:互联网 发布:会计帐套软件 编辑:程序博客网 时间:2024/06/07 09:44

As all know ,CGContextRef 与 CGMutablePathRef 都是画图工具,其中CGMutablePathRef 可与CAShapeLayer或CGContextRef配合使用.


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);


CGMutablePathRef用法


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);



CAShapeLayer或CGContextRef配合使用


CGContextAddPath(context,path);

CGContextDrawPath(context,kCGPathStroke);

CGPathRelease(path);


[shapeLayer setPath:path];

CGPathRelease(path); 


附:


CGContextSetFlatness              设置弯曲的路径中的图形上下文的准确性。
CGContextSetInterpolationQuality  设置图形上下文的插值质量水平。
CGContextSetLineCap               图形环境中的画线的端点的样式设置。
CGContextSetLineDash              设置图形上下文中的虚线的模式。
CGContextSetLineJoin              设置图像上下文中的接接线的样式。
CGContextSetLineWidth             设置图像上下文中的线的宽度。
CGContextSetMiterLimit            设置图像上下文中的连接线的斜接限制。
CGContextSetPatternPhase          设置一个上下文的段落模式。
CGContextSetFillPattern           在指定的图形上下文设置的填充图案模式。
CGContextSetRenderingIntent       在当前图形状态设置渲染意向。
CGContextSetShouldAntialias       设置图形上下文的抗锯齿开启或关闭。
CGContextSetStrokePattern         在指定的图形上下文设置描边图案。




0 0
原创粉丝点击