美文网首页
QurartZ2D_01

QurartZ2D_01

作者: 立刻就爽 | 来源:发表于2016-05-24 17:41 被阅读12次

    Quartz 2D是2D绘图引擎,适用iOS和Mac OS X程序。它提供底层轻量级的接口,并且会根据输出显示设备提供不匹配的逼真的画面。

    Quartz 2D是与分辨率和设备无关的一个引擎。你不需要关注最终成像目标位置如何。非常容易使用,提供大量强大的功能,例如透明度,画线,视角距离效果渲染,颜色管理,反锯齿渲染,PDF文档创建,显示,解析。

    Quartz 2D是Core Graphics框架的一部分,所以看到CG开头,别奇怪,就是Core Graphic的简写。

    #import "DrawView.h"
    
    @interface DrawView ()
    {
        CGPoint _startPoint;
        CGPoint _endPoint;
    }
    @property(nonatomic,strong)NSMutableArray *pathArray;
    @end
    @implementation DrawView
    
    
    - (NSMutableArray *)pathArray{
        if (!_pathArray) {
            self.pathArray = [NSMutableArray array];
        }
        return _pathArray;
    }
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
    //    //获取路径
    //    UIBezierPath *path = [UIBezierPath bezierPath];
    //    [path moveToPoint:_startPoint];
    //    [path addLineToPoint:_endPoint];
    //    [path stroke];
        for (UIBezierPath *path in self.pathArray) {
            [[UIColor redColor] set];
            path.lineWidth = 10;
            [path stroke];
        }
        
        
    }
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        _startPoint = [[touches anyObject] locationInView:self];
        //创建贝瑟尔路径
        UIBezierPath *path = [UIBezierPath bezierPath];
        //起点
        [path moveToPoint:_startPoint];
        //添加到数组
        [self.pathArray addObject:path];
        
    }
    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        _endPoint = [[touches anyObject] locationInView:self];
        //终点
        UIBezierPath *path = [self.pathArray lastObject];
        [path addLineToPoint:_endPoint];
        [self setNeedsDisplay];
       
    }
    /** 第一种画线方式 */
    - (void)line1{
        // 1.获取上下文(绘图环境 画布)
        CGContextRef  cxt = UIGraphicsGetCurrentContext();
        // 2. 描绘路径
        CGMutablePathRef path = CGPathCreateMutable();
        // 起点
        CGPathMoveToPoint(path, NULL, 50, 50);
        // 终点
        CGPathAddLineToPoint(path, NULL, 1000, 1000);
        //3. 把路径放到图形上下文
        CGContextAddPath(cxt, path);
        //4. 渲染上下文
        CGContextStrokePath(cxt);
    
    }
    /** 第二种画线方式 */
    - (void)line2{
        //BezierPath(贝瑟尔路径)
        UIBezierPath *path = [UIBezierPath bezierPath];
        //设置起点
        [path moveToPoint:CGPointMake(50, 50)];
        //设置终点
        [path addLineToPoint:CGPointMake(200, 200)];
        
        
        //改变颜色
        [[UIColor redColor] set];
        //设置宽度
        path.lineWidth = 10;
        //渲染
        [path stroke];
        
        //第二条线
        UIBezierPath *path1 = [UIBezierPath bezierPath];
        [path1 moveToPoint:CGPointMake(200, 200)];
        [path1 addLineToPoint:CGPointMake(200, 400)];
        [[UIColor blueColor] set];
        path1.lineWidth = 10;
        [path1 stroke];
    }
    - (void)line3{
        //绘制曲线
        CGContextRef cxt = UIGraphicsGetCurrentContext();
        //描述路径
        CGContextMoveToPoint(cxt, 50, 50);
        CGContextAddQuadCurveToPoint(cxt,150, 100, 250, 50);
        //渲染
        CGContextStrokePath(cxt);
    }
    @end
    

    相关文章

      网友评论

          本文标题:QurartZ2D_01

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