zoukankan      html  css  js  c++  java
  • 绘图基础、绘图路径

     

    - (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);

    }

  • 相关阅读:
    【SpringMVC】SpringMVC系列15之SpringMVC最佳实践
    【SpringMVC】SpringMVC系列14之SpringMVC国际化
    could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of(maven报错)
    ubuntu14安装tensorflow并测试
    HTMLajax跨域向服务器写入数据
    eclipse的最新版本luna的中建立svn和maven
    关于equals与hashcode的重写
    会计中的冲销和红票
    dubbo在项目中的应用
    dubbo介绍以及创建
  • 原文地址:https://www.cnblogs.com/xiangjune/p/4969460.html
Copyright © 2011-2022 走看看