DrawRect 图形

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DrawRect 图形相关的知识,希望对你有一定的参考价值。

 1 DrawRect
 2 
 3 - (void)drawRect:(CGRect)rect
 4 
 5 {
 6 
 7     // Drawing code
 8 
 9     // 当前视图绘制信息
10 
11     CGContextRef context = UIGraphicsGetCurrentContext();
12 
13     // 设置线条的宽度
14 
15     CGContextSetLineWidth(context, 5);
16 
17     // 设置线条的颜色
18 
19     CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
20 
21 //    // 设置线的起点
22 
23 //    CGContextMoveToPoint(context, 0, 0);
24 
25 //    // 设置一条直线(规定线的终点)
26 
27 //    CGContextAddLineToPoint(context, 280, 280);
28 
29 //    // 设置正方形
30 
31 //    CGContextAddRect(context, CGRectMake(20, 20, 240, 240));
32 
33 //   // 根据绘制信息 在视图上绘制图形
34 
35 //    CGContextStrokePath(context);
36 
37     
38 
39     for (int i = 0; i < self.lineArray.count; i++) {
40 
41         NSMutableArray *points = [self. lineArray objectAtIndex:i];
42 
43         if (0 == points.count) {
44 
45             continue;
46 
47         }
48 
49         for (int j = 0; j < points.count - 1; j++) {
50 
51             NSValue *pointValueA = [points objectAtIndex:j];
52 
53             NSValue *pointValueB = [points objectAtIndex:j + 1];
54 
55             
56 
57             CGPoint pointA = [pointValueA CGPointValue];
58 
59             CGPoint pointB = [pointValueB CGPointValue];
60 
61             
62 
63             CGContextMoveToPoint(context, pointA.x, pointA.y);
64 
65             CGContextAddLineToPoint(context, pointB.x, pointB.y);
66 
67         }
68 
69     }
70 
71     CGContextStrokePath(context);
72 
73 }

 

以上是关于DrawRect 图形的主要内容,如果未能解决你的问题,请参考以下文章

Interface Builder PREVIEW使用IBDesignable和核心图形(drawrect)?

drawRect中一些方法的调用

iOS - drawRect:调用了两次

iOS - 图形上下文绘图

iOS8 问题 - UIView drawRect 未被调用

使用UIBezierPath绘制图形