2010-10-23 102 views
3

我已经成功实施了touchesMoved,现在正在正确画线,我在执行擦除方法时遇到了问题,请协助我在缺乏的地方。如何在此方法中执行擦除/撤消操作

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
mouseSwiped = YES; 

UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
currentPoint.y -= 20; 


UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 

mouseMoved++; 

if (mouseMoved == 10) { 
    mouseMoved = 0; 
} 

} 

- (void) Erase 
{ 
    //NSSet *touches; 
    //UITouch *touch; //= (NSSet *)[touch anyObject]; 
    CGPoint currentPoint; //= [touch locationInView:self.view]; 
    currentPoint.y += 20; 

    if(mouseSwiped) { 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 

    mouseMoved--; 

} 

我在触摸成功实施我的擦除移动,但是当我点击一个,因此结束了,但会删除鼠标并点击离开它停止删除。然后需要再次点击(点击)擦除新的点。那么我怎样才能在移动的触摸上连续擦除,

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

    if (eraseBtnSelected == YES) { 

     if ([touch tapCount] == 1) { 

      [self EraseButton]; 

     } 
    } 
} 

回答

2

不要在事件处理方法中做任何绘图。 永远。使用它们编辑要绘制的东西列表,并在-draw..中完成所有绘图。方法如-drawRect:

要删除某些内容,只需将其从要绘制的内容列表中删除,然后重新绘制相关视图或图层即可。

0

变化以下行

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0); 
1
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0); 

这行代码工作,如果背景色为白色,如果其他那么这是一个问题