1 - (void)drawRect:(CGRect)rect { 2 // Drawing code 3 4 5 //1.获取跟View相关联的上下文 6 CGContextRef ctx = UIGraphicsGetCurrentContext(); 7 //2.描述路径 8 UIBezierPath *path = [UIBezierPath bezierPath]; 9 10 //添加横线 11 [path moveToPoint:CGPointMake(10, 150)]; 12 [path addLineToPoint:CGPointMake(290, 150)]; 13 14 //把当前的状态保存到图片上下文状态栈里. 15 CGContextSaveGState(ctx); 16 17 18 [[UIColor redColor] set]; 19 CGContextSetLineWidth(ctx, 10); 20 21 22 //3.把路径添加到上下文 23 CGContextAddPath(ctx, path.CGPath); 24 25 //4.把上下文的内容渲染出来. 26 CGContextStrokePath(ctx); 27 28 29 path = [UIBezierPath bezierPath]; 30 [path moveToPoint:CGPointMake(150, 10)]; 31 [path addLineToPoint:CGPointMake(150, 290)]; 32 33 34 //恢复上下文状态栈 35 CGContextRestoreGState(ctx); 36 37 38 //3.把路径添加到上下文 39 CGContextAddPath(ctx, path.CGPath); 40 41 //4.把上下文的内容渲染出来. 42 CGContextStrokePath(ctx); 43 44 45 }
浙公网安备 33010602011771号