2013-11-23 23 views
0

嗯,我使用Quartz2D繪製的東西,我跟着這tutorial及其工作正常。但我需要在其中實施UNDO選項。我用Quartz2D畫線,現在到UNDO畫出的線?

我有一個撤銷按鈕,當我按下它時,它必須撤消畫出的線。

我「M使用下面的代碼來繪製。是否有任何人知道它的解決方案。

Thaks提前。

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

mouseSwiped = NO; 
UITouch *touch = [touches anyObject]; 
lastPoint = [touch locationInView:self.view]; 
} 

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

mouseSwiped = YES; 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 

UIGraphicsBeginImageContext(self.view.frame.size); 
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
[self.tempDrawImage setAlpha:opacity]; 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 
} 

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

if(!mouseSwiped) { 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    CGContextFlush(UIGraphicsGetCurrentContext()); 
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 

UIGraphicsBeginImageContext(self.mainImage.frame.size); 
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
self.tempDrawImage.image = nil; 
UIGraphicsEndImageContext(); 
} 

回答

0

你不能撤消具體繪製操作,你必須拯救所有的觸摸並以適當的方式存儲它(你的「step」對象)。你將這個「step」對象存儲在一個繪圖會話(一個數組)中,如果你想做一個撤消操作,你可以刪除這個數組中的最後一個對象並在此之後重新繪製整個圖像,並保留(會話)陣列中的所有步驟。

0

最簡單的撤消方法是捕獲即將更改的矩形,並將該矩形的內容存儲在內存或文件中。當您想要撤消時,只需使用kCGBlendModeCopy繪製保存在正確座標處的矩形。

對於多次撤銷,您可以在需要撤消時存儲這些矩形的堆棧並彈出。重做也很容易,而不是流行音樂,您只需將數組中的一個位置移回即可。對於重做,你前進一個位置。

如果您正在實時修改圖像(即,他們正在用手指進行繪製),那麼您無法事先獲取矩形,而需要第二個緩衝區,其中包含你的圖像,你可以使用它來完成繪製操作後得到你的撤消矩形。完成後,將圖像複製到撤消緩衝區。

祝你好運!