CoreGraphics画线

来源:互联网 发布:淘宝挖宝酱 假货 编辑:程序博客网 时间:2024/05/21 15:38

首先你是要继承自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, [UIColoryellowColor].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, [UIColorgreenColor].CGColor);

    

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

    

    CGContextDrawPath(context,kCGPathFillStroke);

}


3.画圆

- (void)drawRect:(CGRect)rect

{

    // Drawing code

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

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

    

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

    

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

    

    CGContextSetFillColorWithColor(context, [UIColorredColor].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, [UIColoryellowColor].CGColor);//线的颜色,第二个参数是CGColor,需要转一下

    

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

    

    CGContextSetFillColorWithColor(context, [UIColorredColor].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, [UIColoryellowColor].CGColor);//线的颜色,第二个参数是CGColor,需要转一下

    

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

    

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

}





0 0
原创粉丝点击