2012-08-28 123 views
1

我試着在ipad上創建一個簡單的手指繪畫應用程序。我能夠正確繪製一條路徑到屏幕,但我想要一個完全清除所有繪製路徑屏幕的選項。我目前的代碼清除了上下文,但是當我調用繪圖代碼塊時,所有路徑都會重新出現在屏幕上。如何清除CGContextRef?

- (void)drawRect:(CGRect)rect 
{ 
    //Drawing code 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if(clearContext == 0){ 

     CGContextSetLineWidth(context, 6); 
     CGContextSetRGBStrokeColor(context, 1, 0, 0, 1); 
     CGContextAddPath(context, drawingPath); 

     CGContextStrokePath(context); 
    } 
    if(clearContext == 1){ 

     //This is the code I currently have to clear the context, it is clearly wrong 
     //Just used as experimentation. 

     CGContextSetBlendMode(context, kCGBlendModeClear); 
     CGContextAddPath(context, drawingPath); 
     CGContextStrokePath(context); 
     clearContext = 0; 

    } 

} 

回答

4

IM假設drawingPath是CGPath ... 你在清除CGPath?這可能是你的問題

的行動來清除你的路徑

的原因,這樣做:

CGPathRelease(drawingPath); 
drawingPath = CGPathCreateMutable(); 
[self setNeedsDisplay]; 

,並做正常繪製你通常做。如果路徑爲空,則不會繪製任何東西

+0

完美地工作...謝謝! – Michael