CoreGraphics画线

首先你是要继承自UIView的,然后重写drawRcet:(CGRect)rect

先说下面这几个,可以找我要Demo,直接留言即可。

1.画线

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    //都以CG开头

    CGContextRef context = UIGraphicsGetCurrentContext();//创建一块画板,现在这个方法创建的是GetCurrentContext,就是这个Viewframe,也即全屏

    //下面的绘制全都依托与这个context

    

    CGContextMoveToPoint(context, 40, 40);//把画笔移动到一个点,第一个参数就是画板,后两个参数一次为xy的坐标

    CGContextAddLineToPoint(context, 80, 80);//向某个点画一条线,第一个参数就是画板,后两个参数一次为xy的坐标

    

    CGContextSetLineWidth(context, 10);//綫有多粗

    

    CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);//线的颜色,第二个参数是CGColor,需要转一下

    

    CGContextDrawPath(context, kCGPathStroke);//画,第二个参数是都描绘什么,可选择只画线,或者只填充,或者都画

}

2.画框

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextAddRect(context, CGRectMake(40, 40, 40, 40));//画框,第二个参数是一个rect

    

    CGContextSetLineWidth(context, 2);

    

    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);

    

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);//设置填充颜色

    

    CGContextDrawPath(context, kCGPathFillStroke);

}


3.画圆

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();//创建一块画板,现在这个方法创建的是GetCurrentContext,就是这个Viewframe,也即全屏

    //下面的绘制全都依托与这个context

    

    CGContextSetLineWidth(context, 1);//綫有多粗

    

    CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);//线的颜色,第二个参数是CGColor,需要转一下

    

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);//设置填充颜色

    

    CGContextAddEllipseInRect(context, CGRectMake(40, 40, 40, 80));//圆是画在这个框里填充的

    

    CGContextDrawPath(context, kCGPathFillStroke);//画,第二个参数是都描绘什么,可选择只画线,或者只填充,或者都画

}


4.画弧

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();//创建一块画板,现在这个方法创建的是GetCurrentContext,就是这个Viewframe,也即全屏

    //下面的绘制全都依托与这个context

    

    CGContextSetLineWidth(context, 1);//綫有多粗

    

    CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);//线的颜色,第二个参数是CGColor,需要转一下

    

    CGContextAddArc(context, 90, 90, 50, 0, M_PI/3, 0);//第二个参数和第三个参数是弧的圆心,第四个参数radius是半径,第五个参数startAngle是起点角度,第六个参数endAngle是终点的角度,最后一个参数是方向只有0或者1

    

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);//设置填充颜色

    

    CGContextDrawPath(context, kCGPathFill);//画,第二个参数是都描绘什么,可选择只画线,或者只填充,或者都画

}


5.随意画

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();//创建一块画板,现在这个方法创建的是GetCurrentContext,就是这个Viewframe,也即全屏

    //下面的绘制全都依托与这个context

    

    CGContextSetLineWidth(context, 1);//綫有多粗

    

    CGContextMoveToPoint(context, 90, 90);//起点

    CGContextAddLineToPoint(context, 90, 190);

    CGContextAddLineToPoint(context, 190, 190);

    CGContextClosePath(context);//闭合

    

    CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);//线的颜色,第二个参数是CGColor,需要转一下

    

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);//设置填充颜色

    

    CGContextDrawPath(context, kCGPathStroke);//画,第二个参数是都描绘什么,可选择只画线,或者只填充,或者都画

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值