绘图状态一:
- (void)drawRect:(CGRect)rect {
// Drawing code
//oc路径对象绘制
CGContextRef ctx =UIGraphicsGetCurrentContext();
//1.创建路径
UIBezierPath * path = [UIBezierPathbezierPath];
//2.添加子路径
[path moveToPoint:CGPointMake(50,50)];
//2.1 添加线段
[path addLineToPoint:CGPointMake(200,200)];
//2.2 再添加一根线
[path addLineToPoint:CGPointMake(50,200)];
//关闭路径
[path closePath];
//设置线宽
// path.lineWidth = 20;
CGContextSetLineWidth(ctx,20);
//设置线的颜色
[[UIColorgreenColor] set];
//设置线头样式
path.lineCapStyle =kCGLineCapRound;
//设置连接处样式
path.lineJoinStyle =kCGLineJoinRound;
CGContextAddPath(ctx, path.CGPath);
//3.渲染
// [path stroke];
CGContextStrokePath(ctx);
}
- (void) test02
{
//oc路径对象绘制
//1.创建路径
UIBezierPath * path = [UIBezierPathbezierPath];
//2.添加子路径
[path moveToPoint:CGPointMake(50,50)];
//2.1 添加线段
[path addLineToPoint:CGPointMake(200,200)];
//2.2 再添加一根线
[path addLineToPoint:CGPointMake(50,200)];
//关闭路径
[path closePath];
//设置线宽
path.lineWidth =20;
//设置线的颜色
[[UIColorgreenColor] set];
//设置线头样式
path.lineCapStyle =kCGLineCapRound;
//设置连接处样式
path.lineJoinStyle =kCGLineJoinRound;
//3.渲染
[path stroke];
}
- (void) test01
{
//c语言绘制
//1.获取图形上下文对象
CGContextRef ctx =UIGraphicsGetCurrentContext();
//2.添加路径
CGContextMoveToPoint(ctx,50, 50);
//2.1 添加线段
CGContextAddLineToPoint(ctx,200, 200);
//再添加一根线段
CGContextAddLineToPoint(ctx,50, 200);
//关闭路径
CGContextClosePath(ctx);
//设置线宽
CGContextSetLineWidth(ctx,20);
//设置线的颜色
// [[UIColor redColor] setStroke];//stroke时有效
// [[UIColor redColor] setFill];//填充时有效
[[UIColorredColor] set];//不管stroke还是fill都有效
//设置线头样式
// CGContextSetLineCap(ctx, kCGLineCapSquare);
//
// //设置连接处样式
// CGContextSetLineJoin(ctx, kCGLineJoinMiter);
//3.渲染
CGContextStrokePath(ctx);
// CGContextFillPath(ctx);
}
绘图状态二:
- (void)drawRect:(CGRect)rect {
// Drawing code
//绘制圆环
// //1.创建路径
// UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:YES];
//
// //设置线宽及颜色
// path.lineWidth = 20;
//
// [[UIColor redColor] set];
//
//
// //2.渲染
// [path stroke];
//方式2
UIBezierPath * path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:YES];
[[UIColor redColor] set];
[path1 fill];
UIBezierPath * path2 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:80 startAngle:0 endAngle:2 * M_PI clockwise:YES];
// [[UIColor blueColor] set];
[self.backgroundColor set];
[path2 fill];
}
- (void) test03
{
//绘制圆弧
//1.获取图形上下文对象
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.创建路径
//参数1:圆心 参数2:半径 参数3:起始弧度 参数4:结束弧度 参数5:是否顺时针
//设置圆心
CGPoint centerP = CGPointMake(150, 150);
//设置半径
CGFloat radius = 100;
//设置起始弧度和结束弧度
CGFloat start = 0;
CGFloat end = M_PI * 2;
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:start endAngle:end clockwise:YES];
//连接圆心
// [path addLineToPoint:centerP];
//关闭路径
// [path closePath];
//3.把路径添加到上下文中
CGContextAddPath(ctx, path.CGPath);
//4.渲染
CGContextDrawPath(ctx, kCGPathStroke);
}
- (void) test02
{
//绘制椭圆
//1.获取图形上下文对象
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.创建路径
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
//3.把刚刚创建的路径添加到图形上下文中
CGContextAddPath(ctx, path.CGPath);
//4.渲染
CGContextDrawPath(ctx, kCGPathStroke);
}
- (void) test01
{
//绘制带圆角的矩形
//1.获取图形上下文对象
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.创建路径
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 150, 150) cornerRadius:49];
//3.把刚刚创建的路径添加到图形上下文中
CGContextAddPath(ctx, path.CGPath);
//4.渲染
CGContextDrawPath(ctx, kCGPathStroke);
}