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.
source share