2012-12-14 145 views
0

我使用不同的按钮来改变使用CGContext的UIBezierPath曲线图的笔触颜色,但改变颜色先前绘制的线也根据最后的笔触颜色改变颜色。但我不想早期绘制的线条改变颜色。 任何帮助表示赞赏。 我用下面的代码:添加CGContextSaveGState(bluecontext);CGContextRestoreGState(bluecontext);CGContext:改变整个视图的颜色,如果我改变笔触颜色

(void)drawRect:(CGRect)rect{ 

if(colorwith==1){ 
    CGContextRef bluecontext = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(bluecontext); // clears any previous path 
    CGContextSetRGBFillColor(bluecontext, 0.2, 0.3, 0.5, .06); 

    CGContextSetStrokeColorWithColor(bluecontext, [UIColor blueColor].CGColor); 
    CGContextStrokePath(bluecontext); // draw blue line 


for(UIBezierPath *_tempPath in _arrayForOperationPath) 
{ 
    [_tempPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];  
} 
    //[[UIColor blackColor]setStroke]; 
} 
else if (colorwith==2){ 

    CGContextRef bluecontextt = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(bluecontextt); // clears any previous path 
    CGContextSetRGBFillColor(bluecontextt, 0.2, 0.8, 0.7, .01); 

    CGContextSetStrokeColorWithColor(bluecontextt, [UIColor redColor].CGColor); 
    CGContextStrokePath(bluecontextt); 
      // [[UIColor redColor]setStroke]; 


for(UIBezierPath *_tempPath in _arrayForOperationPath) 
{ 
    [_tempPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];  
} 
    //[[UIColor blackColor]setStroke]; 
} 

}

回答

1

CGContextRef bluecontext = UIGraphicsGetCurrentContext();后您绘制贝塞尔路径之后。

您的问题是drawRect这个被称为每次上下文更新...所以每次你画的东西。在这里您可以设置线的颜色,但已经提请以前的路径不记得RGB..just阿尔法(如果我没记错的话)。

CGContextSaveGStateCGContextRestoreGState您保存上一个上下文,绘制,然后恢复上下文,再加上您完成的绘图。