如何移除UIBezierPath绘制?

6

我正在创建一个应用程序,在最终需要签名的地方,我想知道如果用户签名错误,应该如何清除签名?

编辑:

行类:

#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

在我另一个类"SecondViewController"中,我有一个按钮与IBAction clearMethod连接:

-(IBAction)clearMethod:(id)sender{

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

}

它包含了Line类并调用了clearPath方法。

不起作用的部分是clearPath函数内部的内容:

-(void) clearPath{

        path = nil;
        [self setNeedsDisplay];

}
3个回答

6

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


3

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


你应该提供你正在做的代码,并强调哪些部分出了问题。 - Teja Nandamuri
1
每当您想要清除路径时,不应该创建LinearInterpView的新实例。您应该声明一个LinearInterpView属性,并在开始绘制签名时仅实例化一次,然后使用相同的属性调用clearPath方法。 - Teja Nandamuri

1
假设您正在使用 drawRect 来渲染贝塞尔路径,并在将路径绘制到上下文之前清除文本。因此,如果您清除路径(放弃旧路径并创建一个新的空路径),那么您将只绘制一个清晰的矩形...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接