IOS 图形上下文栈

 

- (void)drawRect:(CGRect)rect
{
    
    
    // 获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 保存一份最纯洁的图形上下文
    // 调用一次该方法就会拷贝一个上下文到栈中
    CGContextSaveGState(ctx);
//CGContextSaveGState(ctx);

    
    // 第一条线
    // 利用图形上下文保存绘图信息
    CGContextMoveToPoint(ctx, 150, 20);
    CGContextAddLineToPoint(ctx, 20, 100);
    
    // 设置第一条线的状态
    CGContextSetLineWidth(ctx, 10);
    CGContextSetLineCap(ctx, kCGLineCapRound);
    [[UIColor redColor] set];
    
    // 渲染
    CGContextStrokePath(ctx);
    
    
    // 还原开始保存的那份最纯洁的图形上下文
    CGContextRestoreGState(ctx);
    
    // 第二条线
    CGContextMoveToPoint(ctx, 80, 30);
    CGContextAddLineToPoint(ctx, 80, 150);
    /*
    // 清空状态
    CGContextSetLineWidth(ctx, 5);
    CGContextSetLineCap(ctx, kCGLineCapButt);
    [[UIColor greenColor] set];
    */
   
    // 还原开始保存的那份最纯洁的图形上下文
    CGContextRestoreGState(ctx);
    /*
    // 第3条线
    CGContextMoveToPoint(ctx, 200, 30);
    CGContextAddLineToPoint(ctx, 80, 150);
    */


    // 渲染
    CGContextStrokePath(ctx);
    
}

 

posted on 2017-03-21 21:53  守望星空  阅读(145)  评论(0编辑  收藏  举报

导航