2013-11-23 7 views
0

글쎄 Quartz2D를 사용하여 물건을 그리는 데, 나는이 tutorial과 그 작업 fine.But 따라 UNDO 옵션을 구현해야합니다.저는 Quartz2D를 사용하여 선을 그었습니다. 그려진 선을 실행 취소 했습니까?

내가 누를 때 실행 취소 단추가 있습니다. 그려진 선을 실행 취소해야합니다.

나는 "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

가 특정 그리기 작업을 취소 할 수 없습니다. 당신은 저장해야 ("스텝"객체)이 "스텝"객체를 드로잉 세션 (배열)에 저장하고 실행 취소를 원할 경우이 배열의 마지막 객체를 삭제합니다 그리고 나서 (세션) 배열에 남아있는 모든 단계로 전체 이미지를 다시 그리십시오.

0

실행 취소하는 가장 간단한 방법은 변경하려는 사각형을 캡처하여 해당 사각형의 내용을 메모리 또는 파일에 저장하는 것입니다. 실행 취소하려면 kCGBlendModeCopy를 사용하여 올바른 좌표에 저장된 직사각형을 그립니다.

여러 번의 실행 취소의 경우이 직사각형의 스택을 저장하고 실행 취소 할 때 팝업 할 수 있습니다. 다시 실행은 간단합니다. 팝업 대신 배열의 한 위치로 되돌아갑니다. 다시 실행하려면 한 위치 앞으로 이동하십시오.

이미지를 실시간으로 수정하는 경우 (즉, 손가락으로 그리는 경우) 미리 직사각형을 미리 가져올 수 없으며 대신에 사본을 포함하는 두 번째 버퍼가 필요합니다. 귀하의 이미지, 그리고 당신이 그릴 작업을 마친 후 실행 취소 직사각형을 얻으려면 그것을 사용할 수 있습니다. 작업이 끝나면 이미지를 실행 취소 버퍼로 복사합니다.

행운을 빈다.

관련 문제