iOS手寫簽名

前段時(shí)間有個(gè)朋友讓幫忙寫一個(gè)手寫簽名的功能,自己利用業(yè)余時(shí)間做了一下也算復(fù)習(xí)了下基礎(chǔ)知識,現(xiàn)在整理出來寫到簡書上;
關(guān)于手寫簽名,我先是找了下別人的博客,有一個(gè)寫的就很好,我把地址貼出來,大家可以參考一下
IOS:手寫簽名的實(shí)現(xiàn)實(shí)現(xiàn)了手勢繪制字體,添加文字水印,圖片剪切、圖片壓縮
主要是我的需求比較簡單,看著人家寫的這么詳細(xì) 不忍心抄寫,所以決定自己寫,先上效果圖:

手寫簽名效果圖.gif

我的思路是,在控制器中添加一個(gè)自定義的簽名的View:signatureView,再設(shè)置一個(gè)重簽的按鈕用來清除重寫,一個(gè)確認(rèn)的按鈕,將簽名保存下來;


簽名頁面.png

頁面比較簡單,為了少寫一些成員變量,我把布局也寫在了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)了手寫簽名功能;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,908評論 6 541
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,324評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,018評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,417評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,783評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,960評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,522評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,267評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,471評論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,698評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,204評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,436評論 2 378

推薦閱讀更多精彩內(nèi)容