我如何在单击按钮时从 uiview 中删除 UIBezierPath。?

How will i remove UIBezierPath from the uiview on button click.?

我正在uiview 上做数字签名。我通过此代码正常创建它,但我无法删除按钮上的贝塞尔曲线路径 click.i 我正在分享我的代码请查看我的代码。

#import "Signature.h"

 @implementation Signature {
UIBezierPath *path;
UIImage *incrementalImage;
CGPoint pts[5]; // we now need to keep track of the four points of a Bezier segment and the first control point of the next segment
uint ctr;
  }

  - (id)initWithCoder:(NSCoder *)aDecoder
  {
if (self = [super initWithCoder:aDecoder])
{
    [self setMultipleTouchEnabled:NO];
    [self setBackgroundColor:[UIColor greenColor]];
    path = [UIBezierPath bezierPath];
    [path setLineWidth:2.0];
}
return self;

  }

   - (id)initWithFrame:(CGRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    [self setMultipleTouchEnabled:NO];
    path = [UIBezierPath bezierPath];
    [path setLineWidth:2.0];
}
return self;
   }

 - (void)drawRect:(CGRect)rect
    {
[incrementalImage drawInRect:rect];
[path stroke];
 }

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {
ctr = 0;
UITouch *touch = [touches anyObject];
pts[0] = [touch locationInView:self];
   }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
      {
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
ctr++;
pts[ctr] = p;
if (ctr == 4)
{
    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment

    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

    [self setNeedsDisplay];
    // replace points and get ready to handle the next segment
    pts[0] = pts[3];
    pts[1] = pts[4];
    ctr = 1;
}
      }

   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
   {
[self drawBitmap];
[self setNeedsDisplay];
[path removeAllPoints];
ctr = 0;
  }

  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
 {
NSLog(@"hello");
[self touchesEnded:touches withEvent:event];
      }

    - (void)drawBitmap
      {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);

if (!incrementalImage) // first time; paint background white
{
    UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
    [[UIColor greenColor] setFill];
    [rectpath fill];
}
[incrementalImage drawAtPoint:CGPointZero];
[[UIColor blackColor] setStroke];
[path stroke];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
    }

    - (void)erase {
NSLog(@"Erase Testing...");
 // self->path   = nil;  //Set current path nil
 //path   = [UIBezierPath bezierPath];
// [self setNeedsDisplay];
  }

我从另一个 class 调用取消按钮单击时的擦除方法,但我无法删除 uibezeripath。

使用此代码会对您有所帮助

- (void)resetPath {
    path   = nil;  
    path   = [UIBezierPath bezierPath]; 
    [self setNeedsDisplay]; 
}

subclass.h 文件中创建此方法。从你的 UIViewController class 你可以在需要的时候调用这个方法

- (IBAction)youraction:(id)sender 
{
    [self.subclassedView resetPath];
}

看看我是如何在我的一个项目中使用它的,如果它有帮助的话,

 - (void)drawRect:(CGRect)rect {

    [_path stroke];

 }


- (id)initWithFrame:(CGRect)frame{

self = [super initWithFrame: frame];

if (self) {
    // set Multiple touches Enabled to false for allow only single touch.
    [self setMultipleTouchEnabled: NO];
    _path = [UIBezierPath bezierPath];
    // set Line width.
    [_path setLineWidth:2.0];

 }
 return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

isMouseswipped = NO;  //for touches eneded to make dot
ctr = 0;
UITouch *myTouch = [touches anyObject];
pts[0] = [myTouch locationInView: self];


}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

isMouseswipped = YES;   //for touches eneded to make dot
CustomSignatureViewController *csvc = [[CustomSignatureViewController alloc]init];

UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView: self];
ctr++;
pts[ctr] = p;

if (ctr == 4) {

    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0);
    [_path moveToPoint: pts[0]];
    [_path addCurveToPoint: pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];
    [self setNeedsDisplay];
    pts[0] = pts[3];
    pts[1] = pts[4];
    ctr = 1;

    csvc.status = 1;
    self.status = 1;
  }


}


- (void)erase {

_path   = nil;  

_path   = [UIBezierPath bezierPath]; 
[_path setLineWidth:2.0];
[self setNeedsDisplay];

}

这是 class 自定义 returns uiview 的签名。它是 UIView 的子 class。

所以当需要签名时,我导入这个 class 并实例化它并获得我可以在其上绘制的 uiview 对象。然后使用图形开始图像上下文将该视图捕获为图像!

更新:

在 viewcontroller 中,我正在调用这样的方法,

 -(void)clear : (id)sender{

  [signView erase];

  signView.status = 0;

 }

signView是签名class(如上所述)的对象。 erase 是在签名 class.h 文件中声明的 public 方法。 (注意:签名 class 是 uiview 的子 class,所以它的实例 returns 查看 onject,所以 signView 是 uiview 对象)

希望这会有所帮助:)

尝试使用它来删除 UIBezierPath,

[path removeAllPoints];

如果您也使用 CAShapeLayer *rectLayer = [[CAShapeLayer alloc] init]; 那么也调用此行,

[rectLayer removeFromSuperlayer];