Ios 如何在单击按钮时从uiview中删除UIBezierPath。?
我正在uiview上进行数字签名工作。我用这个代码正常创建了它,但我不能在点击按钮时删除贝塞尔路径。我正在共享我的代码。请查看我的代码Ios 如何在单击按钮时从uiview中删除UIBezierPath。?,ios,objective-c,uiview,drawrect,uibezierpath,Ios,Objective C,Uiview,Drawrect,Uibezierpath,我正在uiview上进行数字签名工作。我用这个代码正常创建了它,但我不能在点击按钮时删除贝塞尔路径。我正在共享我的代码。请查看我的代码 #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
#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];
}
我在从另一个类取消按钮单击时调用erase方法,但无法删除uibezeripath 使用此代码,它将帮助您
- (void)resetPath {
path = nil;
path = [UIBezierPath bezierPath];
[self setNeedsDisplay];
}
在子类的.h
文件中创建此方法。在UIViewController
类中,您可以随时调用此方法
- (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];
}
这是将uiview返回到fraw签名的自定义类。它是UIView
的子类
所以,当需要签名时,我导入这个类并实例化它,得到可以绘制的uiview对象。然后使用graphics BeginImageContext将该视图捕获为图像
更新:
在viewcontroller中,我调用如下方法:
-(void)clear : (id)sender{
[signView erase];
signView.status = 0;
}
signView
是签名类(如上所述)的对象。而erase
是签名类的.h文件中声明的公共方法。(注意:signature类是uiview的子类,所以它的intance返回object上的view,所以signView是uiview对象)
希望这会有所帮助:)尝试使用此选项删除UIBezierPath
[path removeAllPoints];
如果使用CAShapeLayer*rectLayer=[[CAShapeLayer alloc]init]代码>然后也调用这一行
[rectLayer removeFromSuperlayer];
递增图像
是结果图像吗?你正在设置图像视图吗?没有,先生,我只是添加了一个uiview,没有其他内容。我在视图控制器中添加uiview并给出类签名确切的问题是什么?我没有得到你需要的东西,先生,我用上面的代码画签名,但是当我想在点击按钮时删除签名,它不会删除,这是我的解锁代码,我从中调用擦除方法。-(iAction)擦除:(id)发送方{NSLog(@“callPrint”);self->callerase=[[Signature alloc]init];[self->callerase erase];self.Signature.backgroundColor=[UIColor redColor];}您正在从同一类中擦除它吗?或者从其他类调用此方法?请尝试此rootView.layer.sublayers=nil;谢谢你的回答,先生,我需要问一个问题,你从哪里调用erase方法?我从另一个类调用erase方法,它是我的视图控制器。在这个类中,我制作了答案中提到的这个类的实例,我在viewcontroller类中有另一个方法,在绘制签名后从这个视图中捕获图像。我有两个类,一个是签名,第二个是视图控制器。我在视图控制器表单序列图像板上添加uiview,并给出类名签名。在签名类中添加所有代码Sir这里的状态是什么?ismouseswipped是一个布尔值,我想?在回答中检查我的更新。是的状态为布尔值,由于项目中的其他需要,有许多其他代码