图形上下文栈
- (void)drawRect:(CGRect)rect {
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//保存一份纯洁的图形上下文:保存到上下文栈中
CGContextSaveGState(ctx);
//利用图形上下文保存绘图信息
CGContextMoveToPoint(ctx, 150, 50);
CGContextAddLineToPoint(ctx, 20, 100);
//设置第一个path的状态
CGContextSetLineWidth(ctx, 10);
CGContextSetLineCap(ctx, kCGLineCapRound);
[[UIColor cyanColor] set];
CGContextSaveGState(ctx);//再保存一次上下文到栈中:保存到上下文栈中
CGContextStrokePath(ctx);
//还原当前上下文栈中栈顶的图形上下文(取出最后保存的图形上下文)
CGContextRestoreGState(ctx);
CGContextMoveToPoint(ctx, 20, 50);
CGContextAddLineToPoint(ctx, 150, 150);
CGContextStrokePath(ctx);
//还原当前上下文栈中栈顶的图形上下文
CGContextRestoreGState(ctx);
CGContextMoveToPoint(ctx, 40, 60);
CGContextAddLineToPoint(ctx, 200, 200);
CGContextStrokePath(ctx);
//图形上下文栈遵循先入后出的原则,保存n次才能还原n次,否则会报错
}