Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a UIBezierPath drawing?

I'm creating an app where you eventually have to sign, and I was wondering how you would clear the signature if they messed up?

EDIT:

Line Class:

#import "LinearInterpView.h"

@implementation LinearInterpView
{
    UIBezierPath *path; // (3)
}

- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
    if (self = [super initWithCoder:aDecoder])
    {
        self.backgroundColor = UIColor.whiteColor;
        [self setMultipleTouchEnabled:NO]; // (2)
        [self setBackgroundColor:[UIColor whiteColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;
}

- (void)drawRect:(CGRect)rect // (5)
{
    [[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]; // (4)
    [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];
}

-(void) clearPath{

    path = nil;
    path = [UIBezierPath bezierPath];
    [self setNeedsDisplay];

}

@end

And then in my other class "SecondViewController", I have a button which is connected to the IBAction clearMethod:

-(IBAction)clearMethod:(id)sender{

     LinearInterpView *theInstance = [[LinearInterpView alloc] init];
    [theInstance clearPath];

}

It contains the Line Class and calls the clearPath Method.

The part that is not working is what is inside of the clearPath function:

-(void) clearPath{

        path = nil;
        [self setNeedsDisplay];

}
like image 629
DCAdams Avatar asked Mar 11 '23 23:03

DCAdams


2 Answers

To reset the UIBezierPath you should use -removeAllPoints

like image 165
user3230875 Avatar answered Mar 14 '23 11:03

user3230875


set the bezierPath to nil which clears the old bezier path! and call [self setNeedsDisplay] on the view where you draw the signature!

like image 22
Teja Nandamuri Avatar answered Mar 14 '23 12:03

Teja Nandamuri