美文网首页iOS学习笔记
Quartz2D——线段绘制方式

Quartz2D——线段绘制方式

作者: 学长的日常 | 来源:发表于2016-07-28 21:16 被阅读23次

    Quartz2D绘图的步骤: 1.获取上下文 2.创建路径(描述路径) 3.把路径添加到上下文 4.渲染上下文。通常在drawRect方法里面绘图,因为只有在这个方法里面才能获取到跟View的layer相关联的图形上下文。

    • 方式一
    - (void)drawRect:(CGRect)rect {    
          // 1.获取图形上下文
        // 目前我们所用的上下文都是以UIGraphics
        // CGContextRef Ref:引用 CG:目前使用到的类型和函数 一般都是CG开头 CoreGraphics
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        // 2.描述路径
        // 创建路径
        CGMutablePathRef path = CGPathCreateMutable();
        
        // 设置起点
        // path:给哪个路径设置起点
        CGPathMoveToPoint(path, NULL, 50, 50);
        
        // 添加一根线到某个点
        CGPathAddLineToPoint(path, NULL, 200, 200);
        
        // 3.把路径添加到上下文
        CGContextAddPath(ctx, path);
        
        // 4.渲染上下文
        CGContextStrokePath(cox);
    }
    
    • 方式二:
        - (void)drawRect:(CGRect)rect {
        // 获取上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        // 描述路径
        // 设置起点
        CGContextMoveToPoint(ctx, 50, 50);
        
        CGContextAddLineToPoint(ctx, 200, 200);
        
        // 渲染上下文
        CGContextStrokePath(cox);
    }
    
    • 方式三:
        - (void)drawRect:(CGRect)rect {
        // UIKit已经封装了一些绘图的功能
        
        // 贝瑟尔路径
        // 创建路径
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        // 设置起点
        [path moveToPoint:CGPointMake(50, 50)];
        
        // 添加一根线到某个点
        [path addLineToPoint:CGPointMake(200, 200)];
        
        // 绘制路径
        [path stroke];
    }
    

    相关文章

      网友评论

        本文标题:Quartz2D——线段绘制方式

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