How do I remove a UIBezierPath drawing?

2019-06-23 22:00发布

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];

}

3条回答
三岁会撩人
2楼-- · 2019-06-23 22:23

Presumably you're using drawRect to render the bezier path, and clearing the text before drawing the path into the context. So, if you clear the path (throw the old one away and create a new empty one) then you'll just draw a clear rect...

查看更多
▲ chillily
3楼-- · 2019-06-23 22:26

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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-06-23 22:26

To reset the UIBezierPath you should use -removeAllPoints

查看更多
登录 后发表回答