使用Swift代碼實現(xiàn)iOS手勢解鎖、指紋解鎖實例詳解
一、手勢密碼
1、
1.1、用UIButton組成手勢的節(jié)點。
1.2、當手指接觸屏幕時,調用重寫的 touchesBegan:withEvent方法(在touchesBegan里調用setNeedsDisplay,這樣就會自動調用drawRect方法)。
1.3、當手指在屏幕上滑動時,調用重寫的touchesEnded:withEvent方法。
這兩個方法執(zhí)行的操作是一樣的:通過locationInView獲取 觸摸的坐標,然后用 CGRectContainsPoint 判斷手指是否經(jīng)過UIButton,如果經(jīng)過按鈕,就更換按鈕的圖片,同時 保存劃過按鈕的tag。
1.4、默認情況下 跳躍連線 第1個和第3個節(jié)點,中間的第2個節(jié)點 會被忽略,所以要單獨進行處理。根據(jù)1和3節(jié)點 的2個UIButton的坐標 計算出第1個和第3個節(jié)點 中間的坐標,判斷該坐標是否存在UIButton,如果存在就加入設置選中,并加入選中數(shù)組。
到這里 就已經(jīng)實現(xiàn)了 手指滑過 節(jié)點的時候 節(jié)點被選中的效果:
// MARK: - Override // 當手指接觸屏幕時,就會調用touchesBegan:withEvent方法; override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { print("執(zhí)行touchesBegan") selectBtnTagArray.removeAll() touchesChange(touches) } //當手指在屏幕上移動時,調用touchesMoved:withEvent方法; override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { touchesChange(touches) } //當觸摸被取消(比如觸摸過程中被來電打斷),就會調用touchesCancelled:withEvent方法。 override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { } //當手指離開屏幕時,就會調用touchesEnded:withEvent方法; override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { print("執(zhí)行touchesEnded") var alertTitle = "請設置正確的手勢" var alertMessage = "手勢密碼不能少于4個" var isSuccess = false if selectBtnTagArray.count >= 4 { alertTitle = "手勢密碼設置成功" isSuccess = true alertMessage = "密碼為:\(selectBtnTagArray)" } gestureLockDelegate!.gestureLockSuccess(isSuccess, title: alertTitle, message: alertMessage) gesturePoint = CGPointZero; self.setNeedsDisplay() } // MARK: - PrivateMethod private func initButtons() { for i in 0...8 { //第幾行 let row = i / 3 let loc = i % 3 //兩個button的間距 let btnSpace = (screenWidth - 3*btnWH)/4 let btnX = btnSpace + (btnWH + btnSpace) * CGFloat(loc) let btnY = 70 + btnSpace + (btnWH + btnSpace) * CGFloat(row) let gestureNodeBtn = UIButton(frame:CGRectMake(btnX, btnY, btnWH, btnWH)) gestureNodeBtn.tag = i gestureNodeBtn.userInteractionEnabled = false //不響應用戶的交互。一定要加上這句 gestureNodeBtn.setImage(UIImage(named: btnImgNormal), forState: .Normal) self.addSubview(gestureNodeBtn) btnArray.append(gestureNodeBtn) } } private func touchesChange(touches: Set<UITouch>) { //獲取 觸摸對象 ,觸摸對象的位置坐標來實現(xiàn) gesturePoint = touches.first!.locationInView(self) for btn in btnArray { //判斷 手指的坐標 是否在 button的坐標里 if !selectBtnTagArray.contains(btn.tag) && CGRectContainsPoint(btn.frame, gesturePoint) { //處理跳躍連線 var lineCenterPoint:CGPoint = CGPoint() if selectBtnTagArray.count > 0 { lineCenterPoint = centerPoint(btn.frame.origin, endPoint: btnArray[selectBtnTagArray.last!].frame.origin) } //保存中間跳躍 過的節(jié)點 for btn in btnArray { if !selectBtnTagArray.contains(btn.tag) && CGRectContainsPoint(btn.frame, lineCenterPoint) { btn.setImage(UIImage(named: btnImgSelected), forState: .Normal) selectBtnTagArray.append(btn.tag) } } //保存劃過的按鈕的tag selectBtnTagArray.append(btn.tag) btn.setImage(UIImage(named: btnImgSelected), forState: .Normal) } } //setNeedsDisplay會自動調用drawRect方法 進行畫線 self.setNeedsDisplay() } //計算2個節(jié)點中心的坐標 private func centerPoint(startPoint: CGPoint, endPoint:CGPoint) -> CGPoint { let rightPoint = startPoint.x > endPoint.x ? startPoint.x : endPoint.x let leftPoint = startPoint.x < endPoint.x ? startPoint.x : endPoint.x let topPoint = startPoint.y > endPoint.y ? startPoint.y : endPoint.y let bottomPoint = startPoint.y < endPoint.y ? startPoint.y : endPoint.y //x坐標: leftPoint +(rightPoint-leftPoint)/2 = (rightPoint+leftPoint)/2 return CGPointMake((rightPoint + leftPoint)/2 + btnWH/2, (topPoint + bottomPoint)/2 + btnWH/2); } func recoverNodeStatus() { selectBtnTagArray.removeAll() for btn in btnArray { btn.setImage(UIImage(named: btnImgNormal), forState: .Normal) } self.setNeedsDisplay() }
2、畫線:在drawRect方法中進行畫線。
override func drawRect(rect: CGRect) { print("執(zhí)行drawRect") let context = UIGraphicsGetCurrentContext() //獲取畫筆上下文 var i = 0 for tag in selectBtnTagArray { if (0 == i) { //開始畫線,設置直線的起點坐標 CGContextMoveToPoint(context, btnArray[tag].center.x, btnArray[tag].center.y) } else { //畫直線,設置直線的終點坐標 CGContextAddLineToPoint(context, btnArray[tag].center.x,btnArray[tag].center.y) } i = i+1 } //如果有選中的節(jié)點,就取 跟著 手指的滑動 畫線 if (selectBtnTagArray.count > 0) { // 移除最后一條多余的線, if gesturePoint != CGPointZero { CGContextAddLineToPoint(context, gesturePoint.x, gesturePoint.y) } } CGContextSetLineWidth(context, 10) //設置畫筆寬度 CGContextSetLineJoin(context, .Round) //兩個線相交點 平滑處理 CGContextSetLineCap(context, .Round) //設置線條兩端的樣式為圓角 CGContextSetRGBStrokeColor(context, 227/255.0, 54/255.0, 58/255.0, 1) CGContextStrokePath(context) // //對線條進行渲染 }
二、指紋驗證
iPhone 的Home鍵 上的金屬環(huán) 能感應手指,通知Touch ID 讀取指紋,Touch ID 傳感器 可以拍攝 皮膚 皮下層指紋。
每次使用指紋,Touch ID 會持續(xù)的添加新的指紋特性,進一步提高準確、安全性。
Touch ID 不會儲存指紋的圖像。它只存儲指紋的數(shù)學表達式。設備中的芯片還包含稱為“Secure Enclave”的高級安全架構,專門用于保護密碼和指紋數(shù)據(jù)。指紋數(shù)據(jù)通過 Secure Enclave 的專用密鑰得到加密和保護。iOS 和其他 app 絕不會訪問您的指紋數(shù)據(jù),指紋數(shù)據(jù)絕不會存儲到 Apple 服務器、 iCloud 或其他地方。
1、 import LocalAuthentication
2、用 LAContext 類的 canEvaluatePolicy 方法 判斷設備是否支持指紋,然后用 evaluatePolicy 方法 來進行指紋驗證。
執(zhí)行evaluatePolicy方法,系統(tǒng)會自動彈出 驗證指紋的提示框,提示副標題可以自己設定。
如果輸入錯誤后,系統(tǒng)會自動進入“再試一次”的提示框,點擊右邊的 “輸入密碼”選項,要自己寫代碼實現(xiàn) 輸入密碼的彈框。
Demo地址:https://github.com/bugaoshuni/TouchIDAndGestureLock
以上所述是小編給大家介紹的使用Swift代碼實現(xiàn)iOS手勢解鎖、指紋解鎖實例詳解,希望對大家有所幫助!
相關文章
詳解iOS App中UISwitch開關組件的基本創(chuàng)建及使用方法
UISwitch組件就是我們平時在iOS設置菜單中開到的那種左右滑動的開關按鈕,當然我們在開發(fā)時可以進行更多的自定義,這里我們就來詳解iOS App中UISwitch開關組件的基本創(chuàng)建及使用方法2016-05-05iOS實現(xiàn)點擊狀態(tài)欄自動回到頂部效果詳解
在IOS開發(fā)過程中,經(jīng)常會有這種需求,需要通過點擊狀態(tài)欄返回到頂部,給用戶更好的體驗效果,下面這篇文章給大家詳細介紹了實現(xiàn)過程,有需要的可以參考借鑒。2016-09-09IOS實現(xiàn)左右兩個TableView聯(lián)動效果
在我們日常開發(fā)IOS中,經(jīng)常見到兩個tableview的聯(lián)動,滑動一側tableview,另一側tableview跟著滑動,其實實現(xiàn)起來比較簡單,只是需要搞清楚他們之間的區(qū)別和聯(lián)系,下面一起來看看如何實現(xiàn)。2016-08-08iOS開發(fā)之App主題切換解決方案完整版(Swift版)
這篇文章主要為大家詳細介紹了iOS開發(fā)之App主題切換完整解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02舉例講解iOS應用開發(fā)中對設計模式中的策略模式的使用
這篇文章主要介紹了iOS應用設計中對設計模式中的策略模式的使用,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03iOS開發(fā)項目- 基于WebSocket的聊天通訊(2)
這篇文章主要介紹了iOS開發(fā)項目- 基于WebSocket的聊天通訊,可以實現(xiàn)錄音和音樂播放,有需要的可以了解一下。2016-11-11