我正致力于UIView
上的数字签名。我通常通过此代码创建它,但是我无法在按钮单击时删除bezier路径。单击按钮时未创建新的BezierPath
。我正在分享我的代码,请查看我的代码。
//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 :(得分:0)
请将擦除方法改为以下:
- (void)erase
{
[path removeAllPoints];
path = [UIBezierPath bezierPath];
[path setLineWidth:2.0];
[self setNeedsDisplay];
}
要使删除功能起作用,您可以使用以下appocahes:
方法1
如果您要按代码加载签名视图,请使用以下代码:
//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
如果您使用的是storyboard
//ViewController.m
#import "SignView.h "
@interface MySignatureViewController : UIViewController {
@property (nonatomic, weak)SignView* signView;
}
- (IBAction)clearSign:(id)sender {
[self.signView erase];
}