Quartz2D(二)之绘图方式

绘图方式

  • 方式一:C语言的方式

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextMoveToPoint(ctx,50, 50);
        CGContextAddLineToPoint(ctx, 110, 120);
        CGContextAddLineToPoint(ctx, 150, 40);
        CGContextSetLineWidth(ctx, 10);
        CGContextSetLineCap(ctx, 1);
        CGContextSetLineJoin(ctx, 1);
    
        CGContextStrokePath(ctx);
    }
    
  • 方式二:C语言的方式之path

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        // 获取路径
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 50, 50);
        CGPathAddLineToPoint(path, NULL, 110, 120);
        // 将路径添加到上下文中
        CGContextAddPath(ctx, path);
    
        CGContextStrokePath(ctx)
    }
    
  • 方式三:BezierPath

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        UIBezierPath *path = [[UIBezierPath alloc]init];
        [path moveToPoint:CGPointMake(50, 50)];
        [path addLineToPoint:CGPointMake(110, 120)];
        CGContextAddPath(ctx, path.CGPath);
    
        CGContextStrokePath(ctx);
    }
    
  • 方式四:C(CGPath) + OC(UIBezierPath)

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 50, 50);
        CGPathAddLineToPoint(path, NULL, 110, 120);
    
        // 把c的路径CGPath转成oc的路径bezierPath
        UIBezierPath *path1 = [UIBezierPath bezierPathWithCGPath:path];
        [path1 addLineToPoint:CGPointMake(80, 80)];
    
        // 把路径添加到上下文中
        CGContextAddPath(ctx, path1.CGPath);
    
        CGContextStrokePath(ctx);
    
    }
    
  • 方式五:纯OC实现

    - (void)drawRect:(CGRect)rect
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(50, 50)];
        [path addLineToPoint:CGPointMake(110, 120)];
        [path stroke];
    }
    

你可能感兴趣的:(Quartz2D(二)之绘图方式)