Quartz2D
常⽤用拼接路径函数
●新建⼀一个起点
void CGContextMoveToPoint(CGContextRefc,CGFloat x, CGFloat y)
● 添加新的线段到某个点
void CGContextAddLineToPoint(CGContextRefc,CGFloat x, CGFloat y)
● 添加⼀一个矩形
void CGContextAddRect(CGContextRefc,CGRect rect)
● 添加⼀一个椭圆
void CGContextAddEllipseInRect(CGContextRefcontext,CGRect rect)
● 添加⼀一个圆弧
void CGContextAddArc(CGContextRefc,CGFloat x, CGFloat y,
CGFloat radius, CGFloatstartAngle,CGFloat endAngle, int clockwise)
常⽤用绘制路径函数
Mode参数决定绘制的模式
void CGContextDrawPath(CGContextRefc,CGPathDrawingMode mode)
● 绘制空⼼心路径
void CGContextStrokePath(CGContextRefc)
● 绘制实⼼心路径
void CGContextFillPath(CGContextRefc)
提⽰示:⼀一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数, 都是⽤用来绘制路径的
图形上下⽂文栈的操作
●将当前的上下⽂文copy⼀一份,保存到栈顶(那个栈叫做”图形上下⽂文栈”)voidCGContextSaveGState(CGContextRefc)
● 将栈顶的上下⽂文出栈,替换掉当前的上下⽂文
void CGContextRestoreGState(CGContextRefc)
// 获取上下文 view
CGContextRef context = UIGraphicsGetCurrentContext();
// 画点
CGContextMoveToPoint(context, 10, 10);
// 设置线宽度
CGContextSetLineWidth(context, 2);
CGContextSetRGBStrokeColor(context, 1, 1, 0, 1);
// 设置连接点 样式
CGContextSetLineJoin(context, kCGLineJoinBevel);
CGContextSetLineCap(context, kCGLineCapRound);
// 画直线
CGContextAddLineToPoint(context, 10, 100);
CGContextAddLineToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 100, 10);
CGContextAddLineToPoint(context, 10, 10);
// 画矩形
CGContextAddRect(context, CGRectMake(100, 10, 100, 100));
// 平移
CGContextTranslateCTM(context,0,100);
// 缩放
CGContextScaleCTM(context,1.5,1.5);
// 旋转
CGContextRotateCTM(context, M_PI * 0.2);
//
CGContextAddQuadCurveToPoint(context, 100, 200, 10, 10);
// 画圆
CGContextAddEllipseInRect(context, CGRectMake(100, 10, 100, 100));
// 画弧
CGContextAddArc(context, 100, 50, 40, 0 , M_PI, 1);
CGContextAddArc(context, 100, 50, 40, 0 , M_PI, 0);
//渲染到VIew 不填充 填充
CGContextStrokePath(context);
//CGContextFillPath(context);
CGContextClosePath(context);