Quartz2D绘图

以下是具体代码:

-(void)drawline

{

   // 获取上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //起点

    CGContextMoveToPoint(ctx, 10, 20);

    //终点

    CGContextAddLineToPoint(ctx, 100, 300);

    //线宽

    CGContextSetLineWidth(ctx, 20);

    [[UIColor greenColor] set];

    //渲染

    CGContextStrokePath(ctx);

    

    //获取上下文

    CGContextRef a = UIGraphicsGetCurrentContext();

    //起点

    CGContextMoveToPoint(a, 10, 300);

    //终点

    CGContextAddLineToPoint(a, 100, 20);

    //线宽

    CGContextSetLineWidth(a, 20);

    [[UIColor greenColor] set];

    //渲染

    CGContextStrokePath(a);

//    CGContextRef b = UIGraphicsGetCurrentContext();

//    //创建一条直线绘制的路径

//    //注意::凡是通过Quartz2D中带有create/copy/retain方法创建出来的值都必须要释放

//    CGMutablePathRef path = CGPathCreateMutable();

//    //把绘图信息添加到路径里;

//    //起点

//    CGPathMoveToPoint(path, NULL, 60, 60);

//    //终点

//    CGPathAddLineToPoint(path, NULL, 200, 200);

//    //线宽

//    CGContextSetLineWidth(b, 10);

//    //着色

//    [[UIColor redColor] set];

//    //把路径添加到上下文中

//    //把绘制直线的的绘图信息保存到图形上下文中

//    CGContextAddPath(b, path);

//    //渲染

//    CGContextStrokePath(b);

/**以上两种绘图(直线的方式)优缺点:第一种代码阅读性不好,不便于区分,第二种 在使用path时,一个path代表一条路径‘。比如如果要在上下文中绘制多个图形,这种情况下建议使用path*/

 

}

//直线

-(void)drawtu

{//updowncontext

    CGContextRef c = UIGraphicsGetCurrentContext();

    //start

    CGContextMoveToPoint(c, 20, 20);

    //last

    CGContextAddLineToPoint(c, 20, 100);

    CGContextAddLineToPoint(c, 100, 20);

    //close

    CGContextClosePath(c);

    CGContextSetLineWidth(c, 2);

     //fillcolour

    [[UIColor redColor] setFill];

    CGContextDrawPath(c, kCGPathFillStroke);

}

//矩形

-(void)drawjuxing

{

    CGContextRef d = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(d, 30, 40);

    CGContextAddLineToPoint(d, 30, 100);

    CGContextAddLineToPoint(d, 100, 100);

     CGContextAddLineToPoint(d, 100, 40);

    CGContextClosePath(d);

    CGContextSetLineWidth(d, 3);

    [[UIColor greenColor] setFill];

    CGContextDrawPath(d, kCGPathFillStroke);

 

}

//圆

-(void)drawCircle

{

    CGContextRef e = UIGraphicsGetCurrentContext();

    //参数 上下文,圆点,半径 起始角度,结束角度

    CGContextAddArc(e, 70,80 , 50,2*M_PI*3/4, 0, 1);

    CGContextSetLineWidth(e, 5);

   [[UIColor greenColor] set];

    //渲染

    CGContextStrokePath(e);

    CGContextRef f = UIGraphicsGetCurrentContext();

    //参数 上下文,圆点,半径 起始角度,结束角度

    CGContextAddArc(f, 70,80 , 50,0,2*M_PI*3/4,1);

    CGContextSetLineWidth(f, 5);

    [[UIColor blackColor] set];

    //渲染

    CGContextStrokePath(f);

    

 

 

}

//椭圆

-(void)drawellipse

{

    CGContextRef g = UIGraphicsGetCurrentContext();

    //

    CGRect aRect = CGRectMake(20, 20, 160, 100);

    //

    CGContextAddEllipseInRect(g, aRect);

    //渲染

    CGContextStrokePath(g);

}

//曲线

-(void)drawquxian

{

 CGContextRef g = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(g, 2);

    //

 CGContextSetStrokeColorWithColor(g, [UIColor grayColor].CGColor);

    CGContextMoveToPoint(g, 80, 150);

    //

    CGContextAddQuadCurveToPoint(g, 200, 200, 200, 400);

    CGContextStrokePath(g);

  

}

//虚线

-(void)drawxuxian

{

    CGContextRef g = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(g, 5);

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

    CGFloat a[]= {2,6,4,2};

    //

    CGContextSetLineDash(g, 3, a, 4);

    CGContextMoveToPoint(g, 10, 150);

    //

    CGContextAddQuadCurveToPoint(g, 150, 150, 300, 200);

    CGContextStrokePath(g);

    

 }

//文字

-(void)writechinese

{

     CGContextRef g = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(g, 2);

    CGContextSetRGBFillColor(g, 0.5, 0.5, 0.5, .5);

    UIFont * font = [UIFont boldSystemFontOfSize:18.0];

    [@" this is my world"drawInRect:CGRectMake(20, 170, 200, 30) withFont:font];

    CGContextStrokePath(g);

 

 

}

//椭圆

-(void)draw

{//updown

    CGContextRef a = UIGraphicsGetCurrentContext();

    CGRect aRect= CGRectMake(80, 100,210 ,90 );

    CGContextAddEllipseInRect(a, aRect);

    [[UIColor redColor] set];

    //x r

    CGContextStrokePath(a);

}

 

转载于:https://my.oschina.net/u/2893293/blog/737396

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值