Clear UIView drawing?

I use a UIView subclass for drawing, this subclass view is used to get your signature on the view controller. There is a clear button that should clear the UIView, except that it does not work. Here is what I have tried.

subclass.h

@implementation subclassed uiview { UIBezierPath *path; UIImage *incrementalImage; // (1) } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self setMultipleTouchEnabled:NO]; [self setBackgroundColor:[UIColor clearColor]]; path = [UIBezierPath bezierPath]; [path setLineWidth:2.0]; } return self; } - (void)drawRect:(CGRect)rect { [incrementalImage drawInRect:rect]; // (3) [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 // (2) { UITouch *touch = [touches anyObject]; CGPoint p = [touch locationInView:self]; [path addLineToPoint:p]; [self drawBitmap]; // (3) [self setNeedsDisplay]; [path removeAllPoints]; //(4) } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self touchesEnded:touches withEvent:event]; } - (void)drawBitmap // (3) { UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); //[[UIColor blackColor] setStroke]; if (!incrementalImage) { // first draw; UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object [[UIColor clearColor] setFill]; [rectpath fill]; // fill it } [incrementalImage drawAtPoint:CGPointZero]; [path stroke]; incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } @end 

view .m controller

 - (IBAction)clearTapped:(id)sender { self.subclassedView.backgroundColor = [UIColor clearColor]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context,self.subclassedView.bounds); CGContextClearRect(context, self.subclassedView.bounds); CGContextFlush(context); [self.subclassedView setNeedsDisplay]; } 

Can someone tell me why this is not working and what should I do instead?

+7
ios objective-c cocoa-touch uiview drawrect
source share
2 answers

Re-initialize the path variable and force the view to draw using this newly created empty path. This gives the impression of erasing the previous drawn path. Your subclass includes this feature,

 - (void)erase { path = nil; //Set current path nil path = [UIBezierPath bezierPath]; //Create new path [self setNeedsDisplay]; } 

Declare this method in the .h file of your subclass. From the view controller class, you should call this method when you need to erase the view,

 - (IBAction)clearTapped:(id)sender { [self.subclassedView erase]; } 

Give it a try.

Hope this helps!

+15
source share

IT will certainly work .. guaranteed ... addition to @Amar's answer and comment from @Josue ... thanx for both of you

 - (void)erase { path = nil; //Set current path nil path = [UIBezierPath bezierPath]; //Create new path incrementalImage = nil; [self setNeedsDisplay]; } 
+4
source share

All Articles