利用Quartz2D推图的另一个方法 (使用CGMutalePathRef进行分层次)

 

可以利用 CGMutablePathRef 创建每个不同图形,然后再一起添加到CGContext中

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //一、 画线
    // 1.先创建一个路径
    CGMutablePathRef linePath = CGPathCreateMutable();

    // 2.拼接路径
    CGPathMoveToPoint(linePath, nil, 0, 0);
    CGPathAddLineToPoint(linePath, nil, 100, 100);

    // 添加路径到上下文
    CGContextAddPath(ctx, linePath);
    
    //二、 画圆
    CGMutablePathRef circlePath = CGPathCreateMutable();
    CGPathAddEllipseInRect(circlePath, nil, CGRectMake(150, 150, 100, 100));
    CGContextAddPath(ctx, circlePath);
    

    // 渲染
    CGContextStrokePath(ctx);


  // Creat\Copy\retain 用过之后都要做一次release
     CGPathRelease(linePath);
     CGPathRelease(circlePath); 
}

 

 

posted @ 2015-07-02 19:00  第三维  阅读(179)  评论(0编辑  收藏  举报