前段時(shí)間有個(gè)朋友讓幫忙寫一個(gè)手寫簽名的功能棠耕,自己利用業(yè)余時(shí)間做了一下也算復(fù)習(xí)了下基礎(chǔ)知識吹零,現(xiàn)在整理出來寫到簡書上漫雕;
關(guān)于手寫簽名滨嘱,我先是找了下別人的博客,有一個(gè)寫的就很好浸间,我把地址貼出來九孩,大家可以參考一下
IOS:手寫簽名的實(shí)現(xiàn)實(shí)現(xiàn)了手勢繪制字體,添加文字水印发框,圖片剪切、圖片壓縮
主要是我的需求比較簡單煤墙,看著人家寫的這么詳細(xì) 不忍心抄寫梅惯,所以決定自己寫,先上效果圖:
我的思路是仿野,在控制器中添加一個(gè)自定義的簽名的View:signatureView铣减,再設(shè)置一個(gè)重簽的按鈕用來清除重寫,一個(gè)確認(rèn)的按鈕脚作,將簽名保存下來葫哗;
頁面比較簡單,為了少寫一些成員變量球涛,我把布局也寫在了ViewDidLoad中
1劣针、頁面布局的代碼如下:
1.1 頁面布局
self.view.backgroundColor = [UIColor grayColor];
self.signatureView = [[signatureView alloc]initWithFrame:CGRectMake(10, 70, self.view.width - 20, (self.view.width - 20)*0.4)];
self.signatureView.backgroundColor = [UIColor whiteColor];
self.signatureView.color = [UIColor blackColor];
self.signatureView.lineWidth = 2;
[self.view addSubview:self.signatureView];
UIButton *reSignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[reSignBtn setTitle:@"重簽" forState:UIControlStateNormal];
[reSignBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[reSignBtn setFrame:CGRectMake(20, CGRectGetMaxY(self.signatureView.frame)+10, (self.view.width - 20*3)*0.5, 40)];
reSignBtn.layer.cornerRadius = 5.0;
reSignBtn.clipsToBounds = YES;
reSignBtn.layer.borderWidth = 1.0;
reSignBtn.titleLabel.font = [UIFont systemFontOfSize:17];
[reSignBtn addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:reSignBtn];
UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[confirmBtn setTitle:@"確認(rèn)" forState:UIControlStateNormal];
[confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17.0];
confirmBtn.backgroundColor = [UIColor greenColor];
confirmBtn.frame = CGRectMake(CGRectGetMaxX(reSignBtn.frame)+20, reSignBtn.y, reSignBtn.width, reSignBtn.height);
confirmBtn.layer.cornerRadius = 5.0;
confirmBtn.clipsToBounds = YES;
[confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:confirmBtn];
1.2 兩個(gè)按鈕的方法:
// 清除按鈕方法
-(void)clear:(UIButton *)btn{
[self.signatureView clearScreen];
}
// 確定按鈕方法
-(void)confirmBtnClick:(UIButton *)btn {
NSLog(@"確定保存");
// 開啟位圖上下文
UIGraphicsBeginImageContextWithOptions(_signatureView.bounds.size, NO, 0);
// 獲取當(dāng)前位圖上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 截屏內(nèi)容到上下文
[_signatureView.layer renderInContext:ctx];
// 獲取圖片
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
// 關(guān)閉上下文
UIGraphicsEndImageContext();
// 這里拿到簽名后的圖片,可以保存到相冊
// 保存到手機(jī)相冊
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// 我這里是通過block傳值給上一個(gè)頁面
if (self.signBlock) {
self.signBlock(img);
}
[self.navigationController popViewControllerAnimated:YES];
}
如果保存到相冊亿扁,回調(diào)方法:
// 保存相冊成功之后的回調(diào)方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
NSLog(@"保存成功");
// [MBProgressHUD showError:@"保存失敗"];
}else{
// 保存成功
NSLog(@"保存失敗");
//[MBProgressHUD showSuccess:@"保存成功"];
}
}```
###2. signatureView
首先重寫initWithFrame: 設(shè)置默認(rèn)的lineWidth 和color捺典,我們把lineWidth和color設(shè)置成屬性,暴露到.h頭文件中从祝,方便控制器中修改襟己;
-(instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]){
_lineWidth = 1;
_color = [UIColor blackColor];
}
return self;
}
我們在touchesBegan: 和touchesMoved: 中創(chuàng)建和繪制UIBezierPath 路徑
-
(CGPoint)pointWithTouches:(NSSet *)touches
{
// 獲取touch對象
UITouch *touch = [touches anyObject];
// 獲取當(dāng)前觸摸點(diǎn)
return [touch locationInView:self];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{CGPoint curP = [self pointWithTouches:touches];
// 創(chuàng)建路徑
DrawPath *path = [DrawPath pathWitchColor:_color lineWidth:_lineWidth];
[path moveToPoint:curP];
_path = path;[self.paths addObject:path];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint curP = [self pointWithTouches:touches];
[_path addLineToPoint:curP];
[self setNeedsDisplay];
}
上面的DrawPath 是繼承自UIBezierPath 的子類,為了給UIBezierPath拓展color屬性
-
(instancetype)pathWitchColor:(UIColor *)color lineWidth:(CGFloat)lineWidth
{
DrawPath *path = [[self alloc] init];
path.color = color;
path.lineWidth = lineWidth;return path;
}
繪制完成后調(diào)用 [self setNeedsDisplay] 系統(tǒng)會調(diào)用 drawRect:方法完成繪制
-(void)drawRect:(CGRect)rect{
if (self.paths.count == 0) {
return;
}
for (DrawPath *path in self.paths) {
[path.color set];
[path stroke];
}
}
另外的清屏操作和懶加載代碼如下:
// 清屏
-
(void)clearScreen
{
// 清空所有的路徑
[self.paths removeAllObjects];[self setNeedsDisplay];
}
// 懶加載
-(NSMutableArray *)paths{
if (!_path) {
_paths = [NSMutableArray array];}
return _paths;
}
如此我們就用繪圖的方法 簡單實(shí)現(xiàn)了手寫簽名功能牍陌;