How to delete a picture by pressing a button?

I have problems with a graphical application on iOS. I created a free drawing with some tutorials. But I found some difficulties in erasing the picture. In my application, I have a button with an eraser as a background image. After I clicked the erase button, when I draw the drawing, it will erase the drawing wherever I draw. Can anyone help me do this. Thanks in advance.

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); } 

Sorry, what mistake I made.

+6
source share
4 answers

why don’t you implement the same logic in the erasor button as in the draw button. just make the default stroke color in the eraser as white or what color your background is.

0
source

Thus, by drawing, you mean that you have drawn 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 existing color lines. Better tutorial here . It also seemed important.

+1
source

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

then you can draw this layer and then skip the white color to erase it.

It will look like an erase, and your presentation will look as per the requirements.

It will work terribly.

+1
source

Try deleting the drawing in iOS:

 - (IBAction)eraserPressed:(id)sender { red = 255.0/255.0; green = 255.0/255.0; blue = 255.0/255.0; opacity = 1.0; } 
+1
source

Source: https://habr.com/ru/post/955228/


All Articles