1

How, i've this code. When the touch moved, the view adds a line. Now, if i want to create an eraser for this line, how can i do? Please, answer me early!

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

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:drawView];

    UIGraphicsBeginImageContext(drawView.frame.size);
    [drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushDimension);

    const CGFloat *components = CGColorGetComponents([brushColor CGColor]);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    drawView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;   
}
  • this link help you http://stackoverflow.com/questions/3863931/want-to-add-manual-erasing-option-in-ipad-painting-application-by-quartz/12797513#comment19041354_12797513 – Waseem Shah Dec 12 '12 at 07:59

2 Answers2

4

If you are looking for an erase function that the user can use touches to erase portion of the line instead of undo provide by RickyTheCoder, you have 2 options.

  1. The first option is use the brush that has the same background color of the background view so it perceive as line got erased while it actually just got paint over with the color that is same as the background.

  2. The second option is to use the brush with clear color and set the blend mode to clear so it erase the line and the background view is still visible.

    if (isErase) {

    CGContextSetLineWidth(currentContext, 10);
    
    CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetFillColorWithColor(currentContext, [UIColor clearColor].CGColor);
    
    CGContextSetBlendMode(currentContext, kCGBlendModeClear);
    
    CGContextDrawPath(currentContext, kCGPathStroke);
    

    }

Ken W
  • 999
  • 7
  • 10
  • Hi, thank you very very much for your answer! I've insert your code like that: – Tiziano Marano Dec 12 '11 at 22:43
  • And if I have a image view with an image on it with already drawn some drawing. Now if I erase the drawing using this code, it will erase the drawing along with the image revealing the background view. – Shailesh Jan 21 '15 at 11:29
-2

i think this is what you are looking for:

http://soulwithmobiletechnology.blogspot.com/2011/06/redo-undo-in-paint-feature.html

  • 2
    While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) for you to include the essential parts of the linked article in your answer, and provide the link for reference. Failing to do that leaves the answer at risk from link rot. – jscs Dec 10 '11 at 20:12