如何删除UIBezierPath绘图?

时间:2016-06-17 17:59:04

标签: ios objective-c drawing uibezierpath

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

修改

线类:

#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 Class并调用clearPath方法。

无效的部分是clearPath函数内部的部分:

-(void) clearPath{

        path = nil;
        [self setNeedsDisplay];

}

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

据推测,您正在使用drawRect渲染贝塞尔曲线路径,并在将路径绘制到上下文之前清除文本。所以,如果你清理了路径(扔掉旧的路径并创建一个新的路径),那么你只需绘制一个清晰的矩形......