如何在此方法中实现擦除/撤消

时间:2010-10-23 09:34:59

标签: iphone objective-c

我已经成功实现了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];

        }
   }
}

3 个答案:

答案 0 :(得分:2)

不要在事件处理方法中进行任何绘制。 永远。使用它们编辑要绘制的内容列表,并在-draw..中完成所有绘图。像-drawRect:这样的方法。

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

答案 1 :(得分:1)

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

如果背景颜色是白色,那么这行代码就有效,如果是其他问题

答案 2 :(得分:0)

更改以下行

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

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