简体   繁体   中英

Remove a UIBezierPath drawing?

I m working on digital signature on UIView . I create it Normally by this code, but I am not able to remove bezier path on button click. New BezierPath not created when click on button. I am sharing my code please look at my code.

       //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];
        }

Please change erase method to following :

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

        }

To make delete functionality to work you can use below appocahes :

Approach 1

If you are loading sign view by code use the following Code:

//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];
            }

Approach 2

If you are using storyboard

//ViewController.m


        #import "SignView.h "

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



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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM