iOS實(shí)現(xiàn)電子簽名
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)電子簽名的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)原理
1、使用拖動(dòng)手勢(shì)記錄獲取用戶簽名路徑.
2、當(dāng)用戶初次接觸屏幕,生成一個(gè)新的UIBezierPath,并加入數(shù)組中.設(shè)置接觸點(diǎn)為起點(diǎn).在手指拖動(dòng)過(guò)程中為UIBezierPath添加線條,并重新繪制,生成連續(xù)的線.
3、手指滑動(dòng)中不斷的重新繪制,形成簽名效果.
4、簽名完成,轉(zhuǎn)化為UIImage保存.
class CXGSignView: UIView { var path: UIBezierPath? var pathArray: [UIBezierPath] = [] override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = UIColor.gray setupSubviews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setupSubviews() { let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerAction(_:))) self.addGestureRecognizer(panGestureRecognizer) } @objc func panGestureRecognizerAction(_ sender: UIPanGestureRecognizer) { // 獲取當(dāng)前點(diǎn) let currentPoint = sender.location(in: self) if sender.state == .began { self.path = UIBezierPath() path?.lineWidth = 2 path?.move(to: currentPoint) pathArray.append(path!) }else if sender.state == .changed { path?.addLine(to: currentPoint) } self.setNeedsDisplay() } // 根據(jù) UIBezierPath 重新繪制 override func draw(_ rect: CGRect) { for path in pathArray { // 簽名顏色 UIColor.black.set() path.stroke() } } // 清空 func clearSign() { pathArray.removeAll() self.setNeedsDisplay() } // 撤銷 func undoSign() { guard pathArray.count > 0 else { return } pathArray.removeLast() self.setNeedsDisplay() } /// 簽名轉(zhuǎn)化為圖片 func saveSignToImage() -> UIImage? { UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale) guard let context = UIGraphicsGetCurrentContext() else { return nil } self.layer.render(in: context) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS11 SectionHeader 胡亂移動(dòng)且滑動(dòng)時(shí)出現(xiàn)重復(fù)內(nèi)容的解決方法
這篇文章主要介紹了iOS11 SectionHeader 胡亂移動(dòng)且滑動(dòng)時(shí)出現(xiàn)重復(fù)內(nèi)容的解決方法,需要的朋友可以參考下2017-11-11IOS開發(fā)之由身份證號(hào)碼提取性別的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS開發(fā)之由身份證號(hào)碼提取性別的實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07IOS開發(fā) UIAlertController詳解及實(shí)例代碼
這篇文章主要介紹了 IOS開發(fā) UIAlertController詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果
本篇文章主要介紹了ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08iOS中UIAlertController設(shè)置自定義標(biāo)題與內(nèi)容的方法
UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會(huì)有警告)。下面這篇文章主要給大家介紹了關(guān)于iOS中UIAlertController如何設(shè)置自定義標(biāo)題與內(nèi)容的相關(guān)資料,需要的朋友可以參考下。2017-10-10iOS ScrollView實(shí)現(xiàn)自動(dòng)布局的方法(適用Swift 3.0 )
傳說(shuō)中有一個(gè)美工ios開發(fā)者在遇到這個(gè)問(wèn)題的時(shí)候特意跑到蘋果總部去咨詢?nèi)绾螌?duì)scrollview進(jìn)行自動(dòng)布局。當(dāng)然大家不用去了,下面這篇文章就來(lái)給大家介紹關(guān)于iOS ScrollView實(shí)現(xiàn)自動(dòng)布局的方法,文中的語(yǔ)法同樣也適用Swift 3.0 ,需要的朋友可以參考下。2017-12-12Objective-C與Swift之間的互相調(diào)用和跳轉(zhuǎn)
這篇文章主要給大家介紹了關(guān)于Objective-C與Swift之間的互相調(diào)用和跳轉(zhuǎn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05