学习IOS开发UI篇--Quartz2D基本绘图

  Quartz2D绘图的代码步骤

1.获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

2.拼接路径(下面代码是搞一条线段)
CGContextMoveToPoint(ctx, 10, 10);
CGContextAddLineToPoint(ctx, 100, 100);

3.绘制路径
CGContextStrokePath(ctx); // CGContextFillPath(ctx);

  常用拼接路径函数

1.新建一个起点
void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)

2.添加新的线段到某个点
void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y)

3.添加一个矩形
void CGContextAddRect(CGContextRef c, CGRect rect)

4.添加一个椭圆
void CGContextAddEllipseInRect(CGContextRef context, CGRect rect)

5.添加一个圆弧
void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y,
  CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)

  常用绘制路径函数

1.Mode参数决定绘制的模式
void CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)

2.绘制空心路径
void CGContextStrokePath(CGContextRef c)

3.绘制实心路径
void CGContextFillPath(CGContextRef c)

提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数,都是用来绘制路径的

  图形上下文栈的操作

1.将当前的上下文copy一份,保存到栈顶(那个栈叫做”图形上下文栈”)
void CGContextSaveGState(CGContextRef c)

2.替换掉当前的上下文
void CGContextRestoreGState(CGContextRef c)

使用CGContextSclateCTM等方法,一定要在赋值属性前调用

使用CGContextClip时候,一定要在绘制前调用

原文地址:https://www.cnblogs.com/zhaoyan/p/3778943.html