美文网首页
Quartz2D绘图基础篇

Quartz2D绘图基础篇

作者: 有一种再见叫青春 | 来源:发表于2017-04-06 14:31 被阅读36次

    Quartz2D的作用:

    绘制图形:线条\三角形\矩形\园\孤
    绘制文字
    绘制\生成图片
    读取\生成PDF
    截图\裁剪图片
    自定义UI控件

    如何利用Quartz2D绘制东西到view上?
    首先,得有图形上下文,因为它能保存绘图信息,并且决定着绘制到什么地方去.其次,那个图形上下⽂必须跟view相关联,才能将内容绘制到view上面
    ⾃定义view的步骤:
    (1)新建⼀个类,继承自UIView
    (2)实现-(void)drawRect:(CGRect)rect⽅法.然后在这个⽅方法中 :

    1)取得跟当前view相关联的图形上下文;
    2)绘制相应的图形内容
    3)利用图形上下文将绘制的所有内容渲染显示到view上面

    画线

    <pre>`

         // 1.取得和当前视图相关联的图形上下文(因为图形上下文决定绘制的输出目标)/
         // 如果是在drawRect方法中调用UIGraphicsGetCurrentContext方法获取出来的就是Layer的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
         // 2.绘图(绘制直线), 保存绘图信息
         // 设置起点
    CGContextMoveToPoint(ctx, 50, 100);
    
       //设置终点
    CGContextAddLineToPoint(ctx, 50 , 200);
        //设置绘图的状态
    
        //设置颜色
    CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
        //设置线条的宽度
    CGContextSetLineWidth(ctx, 12);
         //设置线条起点和终点的样式为圆角
    CGContextSetLineCap(ctx, kCGLineCapRound);
         //设置线条的转角的样式为圆角
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
         //3.渲染(绘制出一条空心的线)
    CGContextStrokePath(ctx);
    

    `</pre>

    三角型

    <pre>`
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(ctx, 50, 100);
    
    CGContextAddLineToPoint(ctx, 50, 200);
    CGContextAddLineToPoint(ctx, 100, 200);
    
    //设置颜色
    //CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 0.5);
    //[[UIColor redColor]setFill];
    //[[UIColor redColor]setStroke];
    //关闭起点和终点
    CGContextClosePath(ctx);
    CGContextStrokePath(ctx);`</pre>
    

    四边形

    <pre>CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddRect(ctx, CGRectMake(20, 20, 100, 100)); CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); CGContextFillPath(ctx);</pre>

    圆形

    <pre>`CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, // 圆心(x,y)
    CGFloat radius, // 半径
    CGFloat startAngle, CGFloat endAngle, // 开始、结束弧度
    int clockwise // 绘制方向,YES:逆时针;NO:顺时针
    )
    弧度
    中心点右侧 弧度为 0
    中心点下方 弧度为 M_PI_2
    中心点左侧 弧度为 M_PI
    中心点上方 弧度为 -M_PI_2

    ` </pre>

    <pre>第一种方法: CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddArc(ctx, 100, 300, 50, 0, 2*M_PI, 0); CGContextSetRGBFillColor(ctx, 1, 0.25, 0, 1); //CGContextStrokePath(ctx); CGContextFillPath(ctx); 第二种方法 CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 50)); CGContextFillPath(ctx);</pre>

    椭圆

    <pre>CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 150)); CGContextFillPath(ctx);</pre>

    圆弧

    <pre>CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddArc(ctx, 100, 200, 50, M_PI_2, M_PI, 0); CGContextFillPath(ctx);</pre>

    扇形

    <pre>// 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.画饼状图 // 画线 CGContextMoveToPoint(ctx, 100, 100); CGContextAddLineToPoint(ctx, 100, 150); // 画圆弧 CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0); // CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1); // 关闭路径 CGContextClosePath(ctx); CGContextFillPath(ctx);</pre>

    相关文章

      网友评论

          本文标题:Quartz2D绘图基础篇

          本文链接:https://www.haomeiwen.com/subject/cfgfattx.html