QurartZ2D_01

2016-05-24  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读