重写drawRect方法 CGContext的使用

来源:互联网 发布:徐州华道数据招聘 编辑:程序博客网 时间:2024/06/02 13:52

转自:http://blog.csdn.net/learnios/article/details/8978466

使用CGContext 需要在UIView中,覆写 drawRect方法。

1.画一条线段:

- (void)drawRect:(CGRect)rect{

    CGContextRef context = UIGraphicsGetCurrentContext();//获得当前view的图形上下文(context)

       CGContextSetLineWidth(context, 1.0);//制定了线的宽度


          //创建颜色有两种方法,一种很简单,一种可以精细的选择颜色

         (1)

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    CGFloat components[] = {0.0,0.0,1.0,1.0};//颜色元素

    CGColorRef color = CGColorCreate(colorspace, components);//这两行创建颜色

    CGContextSetStrokeColorWithColor(context, color);//使用刚才创建好的颜色为上下文设置颜色

         //这样创建颜色需要注意的是在使用完毕之后需要释放颜色空间
CGColorSpaceRelease(colorspace);

    CGColorRelease(color);

(2)

CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);//也是创建颜色,一句话即可

//创建一条线段

    CGContextMoveToPoint(context, 00);//设置线段起始

    CGContextAddLineToPoint(context, 300300);//设置线段终点


/ /绘制图形

CGContextStrokePath(context);//绘制

/ /***********注意:如果用第一种方法创建颜色的要把释放颜色空间的两句代码写在这里。

} //一条线段就创建完成了。


2.创建一个菱形

- (void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();//获得当前view的图形上下文(context)
        CGContextSetLineWidth(context, 1.0);//制定了线的宽度

CGContextSetStrokeColorWithColor(context, [UIColorblueColor].CGColor);//创建颜色

//创建一个菱形

    CGContextMoveToPoint(context, 100100);

    CGContextAddLineToPoint(context, 150150);

    CGContextAddLineToPoint(context, 100200);

    CGContextAddLineToPoint(context, 50150);

    CGContextAddLineToPoint(context, 100100);

//    //创建一个矩形

//    CGRect rectangle = CGRectMake(60, 170, 200, 80); //x,y坐标,矩形宽,高

//    CGContextAddRect(context, rectangle);


//    //创建一个椭圆

//    CGRect rectangle = CGRectMake(60, 170, 200, 80); //椭圆,圆的绘制是通过定义一个矩形区域,并调用CGContextAddEllipseInRect()方法来完成的。

//    CGContextAddEllipseInRect(context, rectangle);


CGContextStrokePath(context);//绘制

}

3.给路径填充颜色(蓝色边框,红色背景)

//为路径填充颜色的几个函数

    CGContextFillRect(context, rectangl);//填充矩形

    CGContextFillEllipseInRect(context, rectangle);//填充椭圆

    CGContextFillPath(context);//填充路径

/ /在填充颜色之前首先要调用 CGContextSetFillColorWithColor()制定颜色。


- (void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();//获得当前view的图形上下文(context)

CGContextSetLineWidth(context, 1.0);//制定了线的宽度

CGContextSetStrokeColorWithColor(context, [UIColorblueColor].CGColor);//创建颜色

//创建一个矩形

    CGRect rectangle = CGRectMake(6017020080);

    CGContextAddRect(context, rectangle);


CGContextStrokePath(context);//绘制

CGContextSetFillColorWithColor(context,[UIColor redColor].CGColor);

CGContextFillRect(context , rectangle );

}

4.画弧 

//弧线是通过指定2个切点,还有角度,调用CGContextAddArcToPoint()绘制。

- (void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context,2.0);

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

CGContextMoveToPoint(context,100,100);

CGContextAddArcToPoint(context,100,200,300,200,100);

CGContextStrokePath(context);


5.绘制贝兹曲线

//贝兹曲线是通过一个起始点,然后通过两个控制点,还有一个终止点,调用CGContextAddCurveToPoint()函数绘制。

- (void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context,2.0);

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

CGContextMoveToPoint(context,10,10);

CGContextAddCurveToPoint(context,0,50,300,250,300,400);

CGContextStrokePath(context);

}

6.绘制二次贝兹曲线

//通过调用CGContextAddQuadCurveToPoint()来绘制

-(void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context,2.0);

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

CGContextMoveToPoint(context,10,200);

CGContextAddQuadCurveToPoint(context,150,10,300,200);

CGContextStrokePath(context);

}

7.绘制虚线

//通过CGContextSetLineDash()绘制 参数:phase:一个Float类型点数据,表示在第一个虚线绘制的时候跳过多少个点 lengths:一个数组,指明了虚线如何虚实,比如{5,6,4}代表画5个实,6个需,4个实。 count:数组长度

-(void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context,8.0);

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

CGFloat dashArray[] = {2,6,4,2}

CGContextSetLineDash(context,3,dashArray,4); //跳过3个再画虚线,所以刚才开始有 6-(3-2)= 5个虚点

CGContextMoveToPoint(CGContext,100,100);

CGContextAddQuadCurveToPoint(context,150,10,300,200);

CGContextStrokePath(context);

}

8.向Context绘制Image

//绘制image需要制定坐标系统,或者缩放参数。下面将Image绘制到(0,0)位置,全尺寸。

-(void)drawRect:(CGRect)rect{

CGContextRef context = UIGraphicsGetCurrentContext();

UIImage *myImage = [UIImage imageNamed:@"image.png"];

CGPoint myImagePoint = CGPointMake(0,0);

[myImage drawAtPoint:myImagePoint];

[myImage release];

}

//图片适应屏幕大小 drawInRect();

CGRect myImageRect = CGRectMake(10,10,300,400);

[myImage drawInRect:myImageRect];



0 0
原创粉丝点击