iOS 画线 绘制直线、矩形、三角形

本文介绍了如何在iOS应用中使用Quartz 2D进行图形编程,详细展示了如何覆盖drawRect方法来绘制直线、矩形和三角形。通过设置不同的上下文属性,可以改变线条颜色、宽度以及填充颜色。代码示例中,首先演示了如何画文字,然后分别展示了绘制三角形、直线和矩形的步骤。

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

// 覆盖drawRect方法,你可以在此自定义绘画和动画

- (void)drawRect:(CGRect)rect

{

    //An opaque type that represents a Quartz 2D drawing environment.

    //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    /*写文字*/

    CGContextSetRGBFillColor (context,  1, 1, 1, 1.0);//设置填充颜色

    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold"size:15.0f];

    UIColor *redColor = [UIColor redColor];

   

    [self drawInRect:CGRectMake(10, 220, 80, 20) withAttributes: @{NSFontAttributeName:font,NSForegroundColorAttributeName:redColor}];

  

    

    

    

       /*画三角形*/

    //只要三个点就行跟画一条线方式一样,把三点连接起来

    CGPoint sPoints[3];//坐标点

    sPoints[0] =CGPointMake(100, 220);//坐标1

    sPoints[1] =CGPointMake(140, 220);//坐标2

    sPoints[2] =CGPointMake(120, 160);//坐标3

    CGContextAddLines(context, sPoints, 3);//添加线

    CGContextClosePath(context);//封起来

    CGContextDrawPath(context, kCGPathFillStroke); //根据坐标绘制路径

    

  

    

}


- (void)drawRect:(CGRect)rect

{

    //设置背景颜色

    [[UIColor clearColorset];

    UIRectFill([self bounds]);

    //拿到当前视图准备好的画板

    CGContextRef context = UIGraphicsGetCurrentContext();

    //利用path进行绘制三角形

    CGContextBeginPath(context);//标记

        CGContextMoveToPoint(context, self.bounds.size.width / 2, 0);//设置起点

    CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height);

    CGContextAddLineToPoint(context, 0, self.bounds.size.height);

    CGContextClosePath(context);//路径结束标志,不写默认封闭

    [[UIColor redColor] setFill]; //设置填充色

    [[UIColor clearColor] setStroke]; //设置边框颜色

    CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path

    

}


//直线

- (void)drawRect:(CGRect)rect

{

    //获得处理的上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

     //指定直线样式

    CGContextSetLineCap(context, kCGLineCapSquare);

    //直线宽度

    CGContextSetLineWidth(context, 2.0);

    //设置颜色

    CGContextSetRGBStrokeColor(context, 0.314, 0.486, 0.859, 1.0);

    //开始绘制

    CGContextBeginPath(context);

    //画笔移动到点(31,170)

    CGContextMoveToPoint(context, 31, 70);

    //下一点

    CGContextAddLineToPoint(context, 129, 148);

    //下一点

    CGContextAddLineToPoint(context, 159, 148);

    //绘制完成

    CGContextStrokePath(context);

    

    

}

//矩形

- (void)drawRect:(CGRect)rect{

    //创建路径并获取句柄

    CGMutablePathRef path = CGPathCreateMutable();

    //指定矩形

    CGRect rectangle = CGRectMake(10.0f, 10.0f, 200.0f, 300.0f);

    //将矩形添加到路径中

    CGPathAddRect(path,NULL, rectangle);

    //获取上下文

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    //将路径添加到上下文

    CGContextAddPath(currentContext, path);

    //设置矩形填充色

    [[UIColor colorWithRed:0.20f green:0.60f blue:0.80f alpha:1.0f] setFill];

    //矩形边框颜色

    [[UIColor brownColor] setStroke];

    //边框宽度

    CGContextSetLineWidth(currentContext,5.0f);

    //绘制

    CGContextDrawPath(currentContext, kCGPathFillStroke);

    CGPathRelease(path);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值