天天看点

IOS学习之Quartz2D

Quartz2D绘图的代码步骤

1. 获得图形上下文

CGContextRef ctx= UIGraphicsGetCurrentContext();

• 2. 拼接路径(下面代码是搞一条线段)

CGContextMoveToPoint(ctx,10, 10);

CGContextAddLineToPoint(ctx,100, 100);

3. 绘制路径

CGContextStrokePath(ctx);//CGContextFillPath(ctx);

常用拼接路径函数

• 新建一个起点

voidCGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloaty)

• 添加新的线段到某个点

voidCGContextAddLineToPoint(CGContextRefc, CGFloatx, CGFloaty)

• 添加一个矩形

voidCGContextAddRect(CGContextRefc, CGRect rect)

• 添加一个椭圆

voidCGContextAddEllipseInRect(CGContextRefcontext, CGRect rect)

• 添加一个圆弧

voidCGContextAddArc(CGContextRefc, CGFloatx, CGFloaty,

 CGFloatradius, CGFloatstartAngle, CGFloatendAngle, intclockwise)

• Mode 参数决定绘制的模式

voidCGContextDrawPath(CGContextRefc, CGPathDrawingMode mode)

• 绘制空心路径

voidCGContextStrokePath(CGContextRefc)

• 绘制实心路径

voidCGContextFillPath(CGContextRefc)

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

图形上下文栈的操作

• 将当前的上下文 copy 一份 , 保存到栈顶 ( 那个栈叫做 ” 图形上下文栈 ”)

voidCGContextSaveGState(CGContextRef c)

• 将栈顶的上下文出栈 , 替换掉当前的上下文

voidCGContextRestoreGState(CGContextRef c)

继续阅读