2016-11-23 110 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按鈕而不是工作。 –