2011-12-10 40 views
1

如何,我有這段代碼。當觸摸移動時,視圖添加一條線。 現在,如果我想爲這條線創建橡皮擦,我該怎麼辦? 請早日回答我!如何在Xcode中爲此行創建橡皮擦?

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:drawView]; 

    UIGraphicsBeginImageContext(drawView.frame.size); 
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)]; 

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushDimension); 

    const CGFloat *components = CGColorGetComponents([brushColor CGColor]); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]); 

    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    drawView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 
+0

這個鏈接可以幫助您 http://stackoverflow.com/questions/3863931/want-to-添加手動擦除選項在ipad繪畫應用石英/ 12797513#comment19041354_12797513 –

回答

4

如果您正在尋找用戶可以使用觸摸擦除線的一部分,而不是撤消提供擦除功能由RickyTheCoder,你有2個選項。

  1. 第一種選擇是使用具有的 後臺視圖,它認爲的行相同的背景顏色得到了清除刷同時 實際上剛剛與是一樣的背景顏色油漆過。

  2. 第二種方法是使用顏色清晰的筆刷,並將 混合模式設置爲清除,以便擦除線條並且背景視圖仍然可見 。

    如果(isErase) {

    CGContextSetLineWidth(currentContext, 10); 
    
    CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor); 
    
    CGContextSetFillColorWithColor(currentContext, [UIColor clearColor].CGColor); 
    
    CGContextSetBlendMode(currentContext, kCGBlendModeClear); 
    
    CGContextDrawPath(currentContext, kCGPathStroke); 
    

    }

+0

嗨,非常感謝你的回答!我已經插入你的代碼,如下所示: –

+0

如果我有一個帶有圖像的圖像視圖,並且已經繪製了一些圖形。現在,如果我使用此代碼擦除繪圖,它將隨着顯示背景視圖的圖像一起擦除繪圖。 – Shailesh