天天看点

IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

首先了解一下cgcontextref:

an opaque type that represents a quartz 2d drawing environment.

graphics context是图形上下文,可以将其理解为一块画布,我们可以在上面进行绘画操作,绘制完成后,将画布放到我们的view中显示即可,view看作是一个画框.

自己学习时实现的demo,希望对大家有帮助,具体的实现看代码,并有完美的注释解释,还有一些对我帮助的博文供大家参考。都在代码里面。

看一下demo效果图先:

IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

自定义customview类,customview.h:

#import <uikit/uikit.h>  

#import <quartzcore/quartzcore.h>  

#define pi 3.14159265358979323846  

@interface customview : uiview  

@end  

实现类customview.m:

#import "customview.h"  

@implementation customview  

- (id)initwithframe:(cgrect)frame  

{  

    self = [super initwithframe:frame];  

    if (self) {  

    }  

    return self;  

}  

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

- (void)drawrect:(cgrect)rect  

    //an opaque type that represents a quartz 2d drawing environment.  

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

    cgcontextref context = uigraphicsgetcurrentcontext();  

    /*写文字*/  

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

    uifont  *font = [uifont boldsystemfontofsize:15.0];//设置  

    [@"画圆:" drawinrect:cgrectmake(10, 20, 80, 20) withfont:font];  

    [@"画线及孤线:" drawinrect:cgrectmake(10, 80, 100, 20) withfont:font];  

    [@"画矩形:" drawinrect:cgrectmake(10, 120, 80, 20) withfont:font];  

    [@"画扇形和椭圆:" drawinrect:cgrectmake(10, 160, 110, 20) withfont:font];  

    [@"画三角形:" drawinrect:cgrectmake(10, 220, 80, 20) withfont:font];  

    [@"画圆角矩形:" drawinrect:cgrectmake(10, 260, 100, 20) withfont:font];  

    [@"画贝塞尔曲线:" drawinrect:cgrectmake(10, 300, 100, 20) withfont:font];  

    [@"图片:" drawinrect:cgrectmake(10, 340, 80, 20) withfont:font];  

    /*画圆*/  

    //边框圆  

    cgcontextsetrgbstrokecolor(context,1,1,1,1.0);//画笔线的颜色  

    cgcontextsetlinewidth(context, 1.0);//线的宽度  

    //void cgcontextaddarc(cgcontextref c,cgfloat x, cgfloat y,cgfloat radius,cgfloat startangle,cgfloat endangle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度   

    // x,y为圆点坐标,radius半径,startangle为开始的弧度,endangle为 结束的弧度,clockwise 0为顺时针,1为逆时针。  

    cgcontextaddarc(context, 100, 20, 15, 0, 2*pi, 0); //添加一个圆  

    cgcontextdrawpath(context, kcgpathstroke); //绘制路径  

    //填充圆,无边框  

    cgcontextaddarc(context, 150, 30, 30, 0, 2*pi, 0); //添加一个圆  

    cgcontextdrawpath(context, kcgpathfill);//绘制填充  

    //画大圆并填充颜  

    uicolor*acolor = [uicolor colorwithred:1 green:0.0 blue:0 alpha:1];  

    cgcontextsetfillcolorwithcolor(context, acolor.cgcolor);//填充颜色  

    cgcontextsetlinewidth(context, 3.0);//线的宽度  

    cgcontextaddarc(context, 250, 40, 40, 0, 2*pi, 0); //添加一个圆  

    //kcgpathfill填充非零绕数规则,kcgpatheofill表示用奇偶规则,kcgpathstroke路径,kcgpathfillstroke路径填充,kcgpatheofillstroke表示描线,不是填充  

    cgcontextdrawpath(context, kcgpathfillstroke); //绘制路径加填充  

    /*画线及孤线*/  

    //画线  

    cgpoint apoints[2];//坐标点  

    apoints[0] =cgpointmake(100, 80);//坐标1  

    apoints[1] =cgpointmake(130, 80);//坐标2  

    //cgcontextaddlines(cgcontextref c, const cgpoint points[],size_t count)  

    //points[]坐标数组,和count大小  

    cgcontextaddlines(context, apoints, 2);//添加线  

    cgcontextdrawpath(context, kcgpathstroke); //根据坐标绘制路径  

    //画笑脸弧线  

    //左  

    cgcontextsetrgbstrokecolor(context, 0, 0, 1, 1);//改变画笔颜色  

    cgcontextmovetopoint(context, 140, 80);//开始坐标p1  

    //cgcontextaddarctopoint(cgcontextref c, cgfloat x1, cgfloat y1,cgfloat x2, cgfloat y2, cgfloat radius)  

    //x1,y1跟p1形成一条线的坐标p2,x2,y2结束坐标跟p3形成一条线的p3,radius半径,注意, 需要算好半径的长度,  

    cgcontextaddarctopoint(context, 148, 68, 156, 80, 10);  

    cgcontextstrokepath(context);//绘画路径  

    //右  

    cgcontextmovetopoint(context, 160, 80);//开始坐标p1  

    cgcontextaddarctopoint(context, 168, 68, 176, 80, 10);  

    cgcontextmovetopoint(context, 150, 90);//开始坐标p1  

    cgcontextaddarctopoint(context, 158, 102, 166, 90, 10);  

    //注,如果还是没弄明白怎么回事,请参考:http://donbe.blog.163.com/blog/static/138048021201052093633776/  

    /*画矩形*/  

    cgcontextstrokerect(context,cgrectmake(100, 120, 10, 10));//画方框  

    cgcontextfillrect(context,cgrectmake(120, 120, 10, 10));//填充框  

    //矩形,并填弃颜色  

    cgcontextsetlinewidth(context, 2.0);//线的宽度  

    acolor = [uicolor bluecolor];//blue蓝色  

    acolor = [uicolor yellowcolor];  

    cgcontextsetstrokecolorwithcolor(context, acolor.cgcolor);//线框颜色  

    cgcontextaddrect(context,cgrectmake(140, 120, 60, 30));//画方框  

    cgcontextdrawpath(context, kcgpathfillstroke);//绘画路径  

    //矩形,并填弃渐变颜色  

    //关于颜色参考http://blog.sina.com.cn/s/blog_6ec3c9ce01015v3c.html  

    //http://blog.csdn.net/reylen/article/details/8622932  

    //第一种填充方式,第一种方式必须导入类库quartcore并#import <quartzcore/quartzcore.h>,这个就不属于在context上画,而是将层插入到view层上面。那么这里就设计到quartz core 图层编程了。  

    cagradientlayer *gradient1 = [cagradientlayer layer];  

    gradient1.frame = cgrectmake(240, 120, 60, 30);  

    gradient1.colors = [nsarray arraywithobjects:(id)[uicolor whitecolor].cgcolor,  

                        (id)[uicolor graycolor].cgcolor,  

                        (id)[uicolor blackcolor].cgcolor,  

                        (id)[uicolor yellowcolor].cgcolor,  

                        (id)[uicolor bluecolor].cgcolor,  

                        (id)[uicolor redcolor].cgcolor,  

                        (id)[uicolor greencolor].cgcolor,  

                        (id)[uicolor orangecolor].cgcolor,  

                        (id)[uicolor browncolor].cgcolor,nil];  

    [self.layer insertsublayer:gradient1 atindex:0];  

    //第二种填充方式   

    cgcolorspaceref rgb = cgcolorspacecreatedevicergb();  

    cgfloat colors[] =  

    {  

        1,1,1, 1.00,  

        1,1,0, 1.00,  

        1,0,0, 1.00,  

        1,0,1, 1.00,  

        0,1,1, 1.00,  

        0,1,0, 1.00,  

        0,0,1, 1.00,  

        0,0,0, 1.00,  

    };  

    cggradientref gradient = cggradientcreatewithcolorcomponents  

    (rgb, colors, null, sizeof(colors)/(sizeof(colors[0])*4));//形成梯形,渐变的效果   

    cgcolorspacerelease(rgb);  

    //画线形成一个矩形  

    //cgcontextsavegstate与cgcontextrestoregstate的作用  

    /* 

     cgcontextsavegstate函数的作用是将当前图形状态推入堆栈。之后,您对图形状态所做的修改会影响随后的描画操作,但不影响存储在堆栈中的拷贝。在修改完成后,您可以通过cgcontextrestoregstate函数把堆栈顶部的状态弹出,返回到之前的图形状态。这种推入和弹出的方式是回到之前图形状态的快速方法,避免逐个撤消所有的状态修改;这也是将某些状态(比如裁剪路径)恢复到原有设置的唯一方式。 

     */  

    cgcontextsavegstate(context);  

    cgcontextmovetopoint(context, 220, 90);  

    cgcontextaddlinetopoint(context, 240, 90);  

    cgcontextaddlinetopoint(context, 240, 110);  

    cgcontextaddlinetopoint(context, 220, 110);  

    cgcontextclip(context);//context裁剪路径,后续操作的路径  

    //cgcontextdrawlineargradient(cgcontextref context,cggradientref gradient, cgpoint startpoint, cgpoint endpoint,cggradientdrawingoptions options)  

    //gradient渐变颜色,startpoint开始渐变的起始位置,endpoint结束坐标,options开始坐标之前or开始之后开始渐变  

    cgcontextdrawlineargradient(context, gradient,cgpointmake  

                                (220,90) ,cgpointmake(240,110),  

                                kcggradientdrawsafterendlocation);  

    cgcontextrestoregstate(context);// 恢复到之前的context  

    //再写一个看看效果  

    cgcontextmovetopoint(context, 260, 90);  

    cgcontextaddlinetopoint(context, 280, 90);  

    cgcontextaddlinetopoint(context, 280, 100);  

    cgcontextaddlinetopoint(context, 260, 100);  

    cgcontextclip(context);//裁剪路径  

    //说白了,开始坐标和结束坐标是控制渐变的方向和形状  

                                (260, 90) ,cgpointmake(260, 100),  

    //下面再看一个颜色渐变的圆  

    cgcontextdrawradialgradient(context, gradient, cgpointmake(300, 100), 0.0, cgpointmake(300, 100), 10, kcggradientdrawsbeforestartlocation);  

    /*画扇形和椭圆*/  

    //画扇形,也就画圆,只不过是设置角度的大小,形成一个扇形  

    acolor = [uicolor colorwithred:0 green:1 blue:1 alpha:1];  

    //以10为半径围绕圆心画指定角度扇形  

    cgcontextmovetopoint(context, 160, 180);  

    cgcontextaddarc(context, 160, 180, 30,  -60 * pi / 180, -120 * pi / 180, 1);  

    cgcontextclosepath(context);  

    cgcontextdrawpath(context, kcgpathfillstroke); //绘制路径  

    //画椭圆  

    cgcontextaddellipseinrect(context, cgrectmake(160, 180, 20, 8)); //椭圆  

    cgcontextdrawpath(context, kcgpathfillstroke);  

    /*画三角形*/  

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

    cgpoint spoints[3];//坐标点  

    spoints[0] =cgpointmake(100, 220);//坐标1  

    spoints[1] =cgpointmake(130, 220);//坐标2  

    spoints[2] =cgpointmake(130, 160);//坐标3  

    cgcontextaddlines(context, spoints, 3);//添加线  

    cgcontextclosepath(context);//封起来  

    cgcontextdrawpath(context, kcgpathfillstroke); //根据坐标绘制路径  

    /*画圆角矩形*/  

    float fw = 180;  

    float fh = 280;  

    cgcontextmovetopoint(context, fw, fh-20);  // 开始坐标右边开始  

    cgcontextaddarctopoint(context, fw, fh, fw-20, fh, 10);  // 右下角角度  

    cgcontextaddarctopoint(context, 120, fh, 120, fh-20, 10); // 左下角角度  

    cgcontextaddarctopoint(context, 120, 250, fw-20, 250, 10); // 左上角  

    cgcontextaddarctopoint(context, fw, 250, fw, fh-20, 10); // 右上角  

    /*画贝塞尔曲线*/  

    //二次曲线  

    cgcontextmovetopoint(context, 120, 300);//设置path的起点  

    cgcontextaddquadcurvetopoint(context,190, 310, 120, 390);//设置贝塞尔曲线的控制点坐标和终点坐标  

    cgcontextstrokepath(context);  

    //三次曲线函数  

    cgcontextmovetopoint(context, 200, 300);//设置path的起点  

    cgcontextaddcurvetopoint(context,250, 280, 250, 400, 280, 300);//设置贝塞尔曲线的控制点坐标和控制点坐标终点坐标  

    /*图片*/  

    uiimage *image = [uiimage imagenamed:@"apple.jpg"];  

    [image drawinrect:cgrectmake(60, 340, 20, 20)];//在坐标中画出图片  

//    [image drawatpoint:cgpointmake(100, 340)];//保持图片大小在point点开始画图片,可以把注释去掉看看  

    cgcontextdrawimage(context, cgrectmake(100, 340, 20, 20), image.cgimage);//使用这个使图片上下颠倒了,参考http://blog.csdn.net/koupoo/article/details/8670024  

//    cgcontextdrawtiledimage(context, cgrectmake(0, 0, 20, 20), image.cgimage);//平铺图  

用法:

customview *customview = [[customview alloc]initwithframe:cgrectmake(0, 0, 320, self.view.frame.size.height)];  

    [self.view addsubview:customview];  

ok,完成。