Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to erase the drawing using button click?

Tags:

ios

I have some troubles with drawing app in IOS. I have created the free hand drawing with the help of some tutorials. But I found some difficulties in erasing the drawing. In my app, I have button with eraser as background image. After I clicked the eraser button, when I swipes over the drawing, it will erase the drawing wherever I swipes. Can anyone help me to do this. Thanks in advance.

Given below is my code:

@implementation LinearInterpView
{
    UIBezierPath *path;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:YES];
        [self setBackgroundColor:[UIColor whiteColor]];
        path=[UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    [[UIColor blackColor] setStroke];
    [path stroke];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    [path moveToPoint:p];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    [path addLineToPoint:p];
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}

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

    [self touchesEnded:touches withEvent:event];
}

// This is the button action to erase the drawing.
- (IBAction)erase:(id)sender {
    CGContextRef cgref=UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(cgref, kCGBlendModeClear);
}

Kindly clear me, what mistake I did.

like image 756
Ios App developer Avatar asked Oct 04 '13 05:10

Ios App developer


2 Answers

So by drawing you mean you have drew lines on the screen say with some color you can do the same by setting white color and alpha 1 so that white lines replace the existing colored lines. A better tutorial here . This also seemed important.

like image 148
Satheesh Avatar answered Oct 02 '22 13:10

Satheesh


First of all your logic should be make a layer on ImageView.

then you can draw on that layer then pass white color to erase.

It'll look like erase and your view will look like according to requirement.

That will surly work.

like image 24
virantporwal Avatar answered Oct 02 '22 15:10

virantporwal