1 //作用:专门用绘图 2 //什么时候调用:在View显示的时候调用 3 //rect:当前View的bounds 4 - (void)drawRect:(CGRect)rect { 5 6 //无论有没有看到上下文.只要在View上面绘图,就必须在drawRect方法. 7 8 //cornerRadius:圆角半径 9 // UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:50]; 10 11 //画椭圆 12 // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)]; 13 14 //ArcCenter:圆心 15 //radius:圆的半径 16 //startAngle:开始角度//起始点圆的最右侧. 17 //endAngle:截至角度 18 //clockwise:顺时针还是逆时针 YES:顺时针 NO:逆时针 19 20 CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5); 21 CGFloat radius = rect.size.width * 0.5 - 10; 22 CGFloat startA = 0; 23 CGFloat endA = -M_PI_2; 24 25 UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO]; 26 27 [path addLineToPoint:center]; 28 29 //封闭路径 30 // [path closePath]; 31 32 [[UIColor blueColor] set]; 33 34 //fill,会自动把路径给关闭 35 [path fill]; 36 37 } 38 39 //画矩形. 40 - (void)drawrect{ 41 42 //画矩形. 43 //1.获取上下文 44 CGContextRef ctx = UIGraphicsGetCurrentContext(); 45 //2.描述路径 46 UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)]; 47 //3.把路径添加到上下文 48 CGContextAddPath(ctx, path.CGPath); 49 50 [[UIColor yellowColor] set]; 51 52 //4.把上下文的内容显示 53 CGContextFillPath(ctx); 54 55 } 56 57 58 //添加曲线 59 - (void)drawQuadCurve{ 60 61 62 //1.获取跟View相关联的上下文 63 CGContextRef ctx = UIGraphicsGetCurrentContext(); 64 //2.描述路径 65 UIBezierPath *path = [UIBezierPath bezierPath]; 66 //2.1设置起点 67 [path moveToPoint:CGPointMake(20, 200)]; 68 //2.2添加一条曲线到某个点. 69 [path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)]; 70 71 CGContextSetLineWidth(ctx, 10); 72 73 //3.把路径添加到上下文 74 CGContextAddPath(ctx, path.CGPath); 75 //4.把上下文的内容显示出来. 76 CGContextStrokePath(ctx); 77 } 78 79 //画直线 80 - (void)drawLine{ 81 82 // Drawing code 83 NSLog(@"%s",__func__); 84 NSLog(@"%@",NSStringFromCGRect(self.bounds)); 85 86 //1.取得一个跟View相关联的上下文. 87 CGContextRef ctx = UIGraphicsGetCurrentContext(); 88 //2.描述路径 89 UIBezierPath *path = [UIBezierPath bezierPath]; 90 //2.1.设置起点 91 [path moveToPoint:CGPointMake(10, 100)]; 92 //2.1.添加一根线到某个点 93 [path addLineToPoint:CGPointMake(200, 20)]; 94 95 //一个路径上面可以画多条线 96 // [path moveToPoint:CGPointMake(10, 150)]; 97 // [path addLineToPoint:CGPointMake(200, 100)]; 98 99 //把上一条线的终点当作是下一条线的起点. 100 [path addLineToPoint:CGPointMake(150, 200)]; 101 102 103 //设置上下文的状态 104 //设置线的宽度 105 CGContextSetLineWidth(ctx, 10); 106 //设置线的连接样式 107 CGContextSetLineJoin(ctx, kCGLineJoinBevel); 108 //设置顶角的样式 109 CGContextSetLineCap(ctx, kCGLineCapRound); 110 //设置线的颜色 111 [[UIColor greenColor] setStroke]; 112 113 114 //3.把路径添加到上下文 115 CGContextAddPath(ctx, path.CGPath); 116 //4.把上下文的内容显示View fill stroke 117 CGContextStrokePath(ctx); 118 119 120 }
浙公网安备 33010602011771号