*绘制图形
* triangle三角形(画图分析)
* 关闭路径closePath:从路径的终点连接到起点
* 填充路径CGContextFillPath:有了封闭的路径就能填充。
* 设置填充颜色 [[UIColor blueColor] setFill];
* 设置描边颜色 [[UIColor redColor] setStroke];
* 不显示描边颜色,为什么?没有设置线宽
* 设置线宽,还是不显示,为什么?因为绘制路径不对。
* 即填充又描边CGContextDrawPath:kCGPathFillStroke。
* rectangle矩形
* circle圆:为什么传入矩形,因为圆内切与矩形
* arc圆弧
PPT分析:
1> 圆弧属于圆的一部分,因此先要有圆,才有弧。
2> 圆需要起点吗?画线需要,圆也不另外。
3> 起点在哪? 圆心右边
4> 画圆弧还需要起始角度,结束角度,方向,角度必须是弧度
* Quarter 1/4圆
* 画个1/4圆弧,Stroke
* 填充路径,Fill
* 必须有封闭路径才能填充,没有封闭路径,系统会自动关闭路径,再去填充
* 画线连接圆心,自动关闭路径。
2.1 基本图形绘制:
1)1/4圆
- (void)drawRect:(CGRect)rect{
// Drawing code
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
CGPoint center = CGPointMake(125, 125);
CGFloat radius = 100;
CGFloat startA = 0;
CGFloat endA = M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
[path addLineToPoint:center];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
}
2)填充半圆
- (void)drawArc{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
CGPoint center = CGPointMake(125, 125);
CGFloat radius = 100;
CGFloat startA = 0;
CGFloat endA = M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
}
3)圆形
- (void)drawCircle{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 100)];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
}
4)三角形
- (void)drawRectangle{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)];
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:20];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 4.渲染上下文
CGContextStrokePath(ctx);
}
5)封闭三角形
- (void)drawSupernene{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint startP = CGPointMake(10, 10);
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(125, 125)];
[path addLineToPoint:CGPointMake(240, 10)];
// 从路径的终点连接到起点
[path closePath];
// [path addLineToPoint:startP];
// 3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
[[UIColor blueColor] setFill];
[[UIColor redColor] setStroke];
CGContextSetLineWidth(ctx, 15);
// 4.渲染上下文
// CGContextStrokePath(ctx);
// CGContextFillPath(ctx);
// 即填充又描边 kCGPathFillStroke
CGContextDrawPath(ctx, kCGPathFillStroke);
}