Quartz2D基本使用(二)——绘图状态

本文介绍如何使用Objective-C在iOS中实现各种图形绘制,包括基本线条、圆形、椭圆、带圆角的矩形等,并展示了如何调整线条宽度、颜色及样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绘图状态一:

- (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);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值