代码改变世界

绘图基础、绘图路径

2015-11-16 17:10  xiangjune  阅读(269)  评论(0编辑  收藏  举报

 

- (void)drawRect:(CGRect)rect

{

    // 获取context

    CGContextRef context = UIGraphicsGetCurrentContext();

    // 画直线

    CGContextMoveToPoint(context, 50, 100);

    CGContextAddLineToPoint(context, 100, 300);

    CGContextSetLineWidth(context, 5);

    CGContextSetLineCap(context, kCGLineCapRound);

    [[UIColor redColor] set];

    CGContextAddLineToPoint(context, 200, 20);

    CGContextStrokePath(context);

    // 三角形

    CGContextMoveToPoint(context, 10, 10);

    CGContextAddLineToPoint(context, 10, 100);

    CGContextAddLineToPoint(context, 200, 100);

    [[UIColor grayColor]set];

    CGContextSetLineWidth(context, 2);

    CGContextClosePath(context);

    CGContextStrokePath(context);

    // 画四边形

//    CGContextAddRect(context, CGRectMake(100, 100, 200, 50));

//    CGContextStrokePath(context);

    CGContextFillRect(context, CGRectMake(100, 100, 200, 50));

    // 画圆

    CGContextAddArc(context, 250, 250, 50, 0, 2*M_PI, 0);

    CGContextFillPath(context);

//    CGContextStrokePath(context);

    // 画椭圆

    CGContextAddEllipseInRect(context, CGRectMake(300, 200, 100, 200));

//    CGContextStrokePath(context);

    CGContextFillPath(context);

    // 画圆弧

    CGContextAddArc(context, 100, 400, 50, 0, 0.5*M_PI, 0);

    CGContextClosePath(context);

//    CGContextStrokePath(context);

    CGContextFillPath(context);

 

    // 2.画饼状图 --------------------

    // 画线

    CGContextMoveToPoint(context, 100, 100);

    CGContextAddLineToPoint(context, 100, 150);

    // 画圆弧

    CGContextAddArc(context, 100, 100, 50, M_PI_2, M_PI, 0);

    //    CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);

    // 关闭路径

    CGContextClosePath(context);

    [[UIColor brownColor]set];

    // 3.渲染 (注意, 画线只能通过空心来画)

    CGContextFillPath(context);

    

    

    // 图形上下文栈 保存:CGContextSaveGState(context); 恢复:CGContextRestoreGState(context); 在栈里保存了几次,那么就可以取几次(比如不能保存了1次,取两次,在取第二次的时候,栈里为空会直接挂掉)

    

    // 第一条线

    CGContextMoveToPoint(context, 50, 700);

    CGContextAddLineToPoint(context, 50, 800);

    [[UIColor redColor]set];

    CGContextSaveGState(context);

    CGContextStrokePath(context);

    

    // 第二条线

    CGContextMoveToPoint(context, 100, 700);

    CGContextAddLineToPoint(context, 200, 800);

    [[UIColor grayColor]set];

    CGContextStrokePath(context);

    

    // 第三条线,用第一条线的样式

    CGContextMoveToPoint(context, 150, 800);

    CGContextAddLineToPoint(context, 180, 920);

    CGContextRestoreGState(context);

    CGContextStrokePath(context);

    

    // 第四条线,用第一条线的样式, 只保存了一次,但是第二次用,崩溃....

    CGContextMoveToPoint(context, 220, 800);

    CGContextAddLineToPoint(context, 180, 920);

//    CGContextRestoreGState(context);

    CGContextStrokePath(context);

 

 

    // 旋转角度

    CGContextRotateCTM(context, M_PI_4);

    CGContextAddRect(context, CGRectMake(200, 200, 500, 100));

    CGContextStrokePath(context);

    

    // 缩放

    CGContextScaleCTM(context, 2, 0.5);

    CGContextAddRect(context, CGRectMake(100, 100, 200, 200));

    CGContextStrokePath(context);

    

    // 平移

    CGContextTranslateCTM(context, -100, 100);

    CGContextAddRect(context, CGRectMake(100, 100, 200, 200));

    CGContextStrokePath(context);

}

 

 

- (void)drawRect:(CGRect)rect

{

    // 获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    // 1.绘制一条直线

    // 1.1 创建绘图路径

    CGMutablePathRef path1 = CGPathCreateMutable();

    // 1.2 将绘图信息保存至绘图路径中

    CGPathMoveToPoint(path1, NULL, 100, 200);

    CGPathAddLineToPoint(path1, NULL, 200, 300);

    // 1.3将绘图路径保存至上下文中

    CGContextAddPath(context, path1);

    

    // 2.绘制一个矩形

    // 2.1创建绘图路径

    CGMutablePathRef path2 = CGPathCreateMutable();

    // 2.2 将绘图信息保存至绘图路径

    CGPathAddRect(path2, NULL, CGRectMake(200, 200, 200, 100));

    // 2.3 将绘图路径保存至上下文中

    CGContextAddPath(context, path2);

    

    // 绘制

    CGContextStrokePath(context);

    

    // 释放路径

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

    CGPathRelease(path1);

    CGPathRelease(path2);

}