iOS DrawRect简单使用

这篇博客介绍了如何使用DrawRect在iOS中绘制表格,包括画线、矩形、圆、弧线和文本的基本步骤,是初学者理解DrawRect的入门指南。

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

     今天发现有些人被表格所难住了,我的想法是用 DrawRect进行表格的绘制,那么问题来了,对于 DrawRect我感觉只有大神对其会比较了解,于是我就试着写下关于 DrawRect的简单用法,至于更深的,还需要话时间去研究,今天只带来其最基本的用法,供大家有所了解!

   首先我们需要建一个基于 UIView 的类(因为需要在 view 上划线,显示)
   .h:

#import <UIKit/UIKit.h>

 

@interface OneView : UIView


@end


  .m:

 

#import "OneView.h"


@implementation OneView



// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

    [self drawLine];

    

    

}

#pragma mark - 画线

-(void)drawLine{

    //第一步:获取上下文

    //CGContextRef 用来保存图形信息.输出目标

    CGContextRef context = UIGraphicsGetCurrentContext();

    //第二步:画图形

    //设置线的颜色

    CGContextSetRGBStrokeColor(context, 255/255.0, 255/255.0, 255/255.0, 1);

    //设置线的宽度

    CGContextSetLineWidth(context, 13);

    //设置连接点得样式

    CGContextSetLineJoin(context, kCGLineJoinRound);

    //设置线头尾的样式

    CGContextSetLineCap(context, kCGLineCapRound);

    //起点

    CGContextMoveToPoint(context, 10, 20);

    //画线

    CGContextAddLineToPoint(context, 100, 100);

    

    CGContextAddLineToPoint(context, 100, 150);

    

    //第三步:渲染到视图上

    CGContextStrokePath(context);

 

    

}

- (void)drawARect{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(context, 10, 10);

    CGContextAddLineToPoint(context, 110, 10);

    CGContextAddLineToPoint(context, 110, 110);

    CGContextAddLineToPoint(context, 10, 110);

    CGContextAddLineToPoint(context, 10, 10);

    

    

    //第二种

    CGContextAddRect(context, CGRectMake(0, 0, 100, 100));

    CGContextStrokePath(context);

    //填充

//    CGContextFillPath(context);

    

    //只画线

    CGContextStrokePath(context);

    

}

//画圆

- (void)drawCircle{

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextAddEllipseInRect(context, CGRectMake(0, 0, 100, 100));

    

    

    

    CGContextStrokePath(context);



}


- (void)drawArc{

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    /**

     *  <#Description#>

     *

     *  @param c#>          上下文 description#>

     *  @param x#>          圆点x坐标 description#>

     *  @param y#>          圆点y坐标 description#>

     *  @param radius#>     半径 description#>

     *  @param startAngle#> 开始角度 description#>

     *  @param endAngle#>   结束角度 description#>

     *  @param clockwise#>  是否顺时针 description#>

     */

    

    CGContextMoveToPoint(context, 100, 100);

    CGContextAddArc(context, 100, 100, 60, 0, M_PI_4, 0);

    //闭合

    CGContextClosePath(context);

    

    CGContextStrokePath(context);


}



- (void)drawText{

    NSString *string = @"SDFDFWDF:FEEFF:DFWFEFW";

    [string drawAtPoint:CGPointMake(100, 100) withAttributes:nil];

    

    [string drawInRect:CGRectMake(0, 0, 100, 100) withAttributes:nil];

    

    

    

}


- (void)drawBeize{

    //贝塞尔曲线

    UIBezierPath *bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:60 startAngle:0 endAngle:3.14*2 clockwise:YES];

    UIColor *color = [UIColor orangeColor];

    [color setFill];

    

    [bezier setLineWidth:20];

    [bezier fill];

    

}



         到这,基本的画图工作完成了,剩下的就是调用它,让他显示就好了
          viewController 里:

#import "ViewController.h"

#import "OneView.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    OneView *one = [[OneView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

    one.center = self.view.center;

    one.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:one];

    

    

    

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值