【问题标题】:How do I remove a UIBezierPath drawing?如何删除 UIBezierPath 绘图?
【发布时间】:2016-06-17 17:59:03
【问题描述】:

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

编辑:

线类:

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

}

【问题讨论】:

    标签: ios objective-c drawing uibezierpath


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您应该提供您正在做的事情的代码并突出显示不适合您的内容。
      • 你不应该在想要清除路径的时候创建一个新的 LinearInterpView 实例。你应该声明一个 LinearInterpView 的属性,并且在开始绘制签名时只实例化一次,并使用相同的属性调用 clearPath 方法。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        • 2018-01-08
        相关资源
        最近更新 更多