2016-11-23 85 views
0

我在致力于数字签名UIView。我通过这段代码创建它,但我无法删除按钮单击时的贝塞尔路径。点击按钮时不会创建新的BezierPath。我分享我的代码,请看我的代码。删除UIBezierPath绘图?

 //Create Class for UIView 
     #import "SignView.h" 
     { 
      UIBezierPath *path; 
     } 
     - (id)initWithCoder:(NSCoder *)aDecoder 
     { 
      if (self = [super initWithCoder:aDecoder]) 
      { 
       [self setMultipleTouchEnabled:NO]; 
       [self setBackgroundColor:[UIColor whiteColor]]; 
       path = [UIBezierPath bezierPath]; 
       [path setLineWidth:2.0]; 
      } 
      return self; 
     } 

     - (void)drawRect:(CGRect)rect 
     { 
      [[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]; 
      [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)erase 
     { 
      path = nil; 
      path = [UIBezierPath bezierPath]; 
      [path setLineWidth:2.0]; 
      [self setNeedsDisplay]; 

     } 

     //viewController.m 

     - (IBAction)clearSign:(id)sender { 
      SignView *clrView = [[SignView alloc]init]; 
      [clrView erase]; 
     } 
+0

你通过加载代码或staorybaord迹象视图。在您的操作中,标记视图的重新初始化未能清除标记 – Vinodh

+0

不要删除路径对象。使用removeAllPoints函数。 –

+0

@deepak哪种方法可以修复您的问题或清除代码更改 – Vinodh

回答

0

请更改擦除方法如下:

- (void)erase 
     { 
      [path removeAllPoints]; 
      path = [UIBezierPath bezierPath]; 
      [path setLineWidth:2.0]; 
      [self setNeedsDisplay]; 

     } 

要删除功能工作,你可以在下面使用appocahes:

方法1

如果您正在通过代码使用t加载符号视图他下面的代码:

//ViewController.m 

    #import "SignView.h " 

    @interface MySignatureViewController : UIViewController { 
     SignView* signView; 
    } 

    -(void)viewDidLoad{ 
     signView= [[ mySmoothLineView alloc] initWithFrame: desiredFrame]; 
     [signView setBackgroundColor:[UIColor clearColor]]; 
     [self.view addSubview: signView]; 
    } 

    - (IBAction)clearSign:(id)sender { 
       [signView erase]; 
      } 

方法2

如果您使用的故事板

//ViewController.m 


     #import "SignView.h " 

     @interface MySignatureViewController : UIViewController { 
      @property (nonatomic, weak)SignView* signView; 
     } 



     - (IBAction)clearSign:(id)sender { 
        [self.signView erase]; 
       } 
+0

方法1工作,但添加signView viewdidLoad方法来启动应用程序比做标志不工作。按下clearSign按钮而不是工作。 –