views:

28

answers:

2

I have successfully implemented touchesMoved and it is drawing line properly now I am facing problem in implementing erase method, please assist me where I am lacking.

- (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--;

}
+1  A: 

Don't do any drawing in your event-handling methods. Ever. Use them to edit a list of things to draw, and do all of your drawing in -draw... methods like -drawRect:.

To erase something, just delete it from your list of things to draw, and redraw the view or layer in question.

NSResponder
A: 

I have implemented my erases successfully in touches moved but when I tap one so it erases but on mouse over and tap left it stop erasing. Then need to click(tap) again for erasing at new point. So how can I continously erases on touches moved,

  • (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (eraseBtnSelected == YES) { if ([touch tapCount] == 1) {

     [self EraseButton];
    
    
    }
    

    } }