如何删除 UIBezierPath 绘图?

How do I remove a UIBezierPath drawing?

我正在创建一个你最终必须签名的应用程序,我想知道如果他们搞砸了你将如何清除签名?

编辑:

第 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

然后在我的另一个 class "SecondViewController" 中,我有一个连接到 IBAction clearMethod 的按钮:

-(IBAction)clearMethod:(id)sender{

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

}

它包含第 Class 行并调用 clearPath 方法。

不工作的部分是 clearPath 函数内部的部分:

-(void) clearPath{

        path = nil;
        [self setNeedsDisplay];

}

bezierPath 设置为 nil 以清除旧的贝塞尔曲线路径!并在您绘制签名的视图上调用 [self setNeedsDisplay]

大概您正在使用 drawRect 来呈现贝塞尔曲线路径,并在将路径绘制到上下文中之前清除文本。所以,如果你清除路径(扔掉旧路径并创建一个新的空路径)那么你只需绘制一个清晰的矩形...

要重置 UIBezierPath,您应该使用 -removeAllPoints