/**
* 画两条不用样式的线
*
* 在获取图形上下文之后,通过 CGContextSaveGState(ctx); 方法,把当前获取的上下文拷贝一份,保存一份最纯洁的图形上下文。
在画第二条线之前,使用CGContextRestoreGState(ctx);方法,还原开始的时候保存的那份最纯洁的图形上下文。
*/
- (void)drawRect:(CGRect)rect
{
CGContextRef ref = UIGraphicsGetCurrentContext();
// 保存一份最初的图形上下文
CGContextSaveGState(ref);
// 绘制线条一
CGContextMoveToPoint(ref, 10, 50.0);
CGContextAddLineToPoint(ref, 200, 300);
// 宽
CGContextSetLineWidth(ref, 10.0);
// 颜色
[[UIColor redColor] set];
// 两端的样式为圆角
CGContextSetLineCap(ref, kCGLineCapRound);
// 渲染(画线条一)
CGContextStrokePath(ref);
// 画线条二
// 拿到保存的图形上下文
CGContextRestoreGState(ref);
CGContextMoveToPoint(ref, 200, 30);
CGContextAddLineToPoint(ref, 20, 220);
// 也可以重新设置宽、颜色、样式等
// CGContextSetLineWidth(ref, 1.0);
// [[UIColor blueColor] set];
// CGContextSetLineCap(ref, kCGLineCapRound);
CGContextStrokePath(ref);
}