一、画线段
1 - (void)drawRect:(CGRect)rect 2 { 3 // Drawing code 4 // 1.获得图形上下文 5 CGContextRef ctx = UIGraphicsGetCurrentContext(); 6 7 // 2.拼接图形(路径) 8 // 设置线段宽度 9 CGContextSetLineWidth(ctx, 10); 10 11 // 设置线段头尾部的样式 12 CGContextSetLineCap(ctx, kCGLineCapRound); 13 14 // 设置线段转折点的样式 15 CGContextSetLineJoin(ctx, kCGLineJoinRound); 16 17 18 /** 第1根线段(红色) **/ 19 // 设置颜色 20 CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1); 21 // 设置一个起点 22 CGContextMoveToPoint(ctx, 10, 10); 23 // 添加一条线段到(100, 100) 24 CGContextAddLineToPoint(ctx, 100, 100); 25 26 // 3.渲染显示到view上面(渲染一次) 27 CGContextStrokePath(ctx); 28 29 //------------------------ 30 31 /** 第2根线段(蓝色) **/ 32 // 设置颜色 33 CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1); 34 // 设置一个起点 35 CGContextMoveToPoint(ctx, 200, 190); 36 // 添加一条线段到(150, 40) 37 CGContextAddLineToPoint(ctx, 150, 40); 38 CGContextAddLineToPoint(ctx, 120, 60); 39 40 41 // 3.渲染显示到view上面 42 CGContextStrokePath(ctx); 43 }