欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Swift代碼實(shí)現(xiàn)iOS手勢(shì)解鎖、指紋解鎖實(shí)例詳解

 更新時(shí)間:2016年06月03日 09:32:25   作者:云端之巔  
這篇文章主要介紹了使用Swift代碼實(shí)現(xiàn)iOS手勢(shì)解鎖、指紋解鎖的實(shí)現(xiàn)過程,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧

一、手勢(shì)密碼

1、

1.1、用UIButton組成手勢(shì)的節(jié)點(diǎn)。

1.2、當(dāng)手指接觸屏幕時(shí),調(diào)用重寫的 touchesBegan:withEvent方法(在touchesBegan里調(diào)用setNeedsDisplay,這樣就會(huì)自動(dòng)調(diào)用drawRect方法)。

1.3、當(dāng)手指在屏幕上滑動(dòng)時(shí),調(diào)用重寫的touchesEnded:withEvent方法。

這兩個(gè)方法執(zhí)行的操作是一樣的:通過locationInView獲取 觸摸的坐標(biāo),然后用 CGRectContainsPoint 判斷手指是否經(jīng)過UIButton,如果經(jīng)過按鈕,就更換按鈕的圖片,同時(shí) 保存劃過按鈕的tag。

1.4、默認(rèn)情況下 跳躍連線 第1個(gè)和第3個(gè)節(jié)點(diǎn),中間的第2個(gè)節(jié)點(diǎn) 會(huì)被忽略,所以要單獨(dú)進(jìn)行處理。根據(jù)1和3節(jié)點(diǎn) 的2個(gè)UIButton的坐標(biāo) 計(jì)算出第1個(gè)和第3個(gè)節(jié)點(diǎn) 中間的坐標(biāo),判斷該坐標(biāo)是否存在UIButton,如果存在就加入設(shè)置選中,并加入選中數(shù)組。

到這里 就已經(jīng)實(shí)現(xiàn)了 手指滑過 節(jié)點(diǎn)的時(shí)候 節(jié)點(diǎn)被選中的效果:

 // MARK: - Override
 // 當(dāng)手指接觸屏幕時(shí),就會(huì)調(diào)用touchesBegan:withEvent方法;
 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  print("執(zhí)行touchesBegan")
  selectBtnTagArray.removeAll()
  touchesChange(touches)
 }
 //當(dāng)手指在屏幕上移動(dòng)時(shí),調(diào)用touchesMoved:withEvent方法;
 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
  touchesChange(touches)
 }
 //當(dāng)觸摸被取消(比如觸摸過程中被來電打斷),就會(huì)調(diào)用touchesCancelled:withEvent方法。
 override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
 }
 //當(dāng)手指離開屏幕時(shí),就會(huì)調(diào)用touchesEnded:withEvent方法;
 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
  print("執(zhí)行touchesEnded")
  var alertTitle = "請(qǐng)?jiān)O(shè)置正確的手勢(shì)"
  var alertMessage = "手勢(shì)密碼不能少于4個(gè)"
  var isSuccess = false
  if selectBtnTagArray.count >= 4 {
   alertTitle = "手勢(shì)密碼設(shè)置成功"
   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
   //兩個(gè)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 //不響應(yīng)用戶的交互。一定要加上這句
   gestureNodeBtn.setImage(UIImage(named: btnImgNormal), forState: .Normal)
   self.addSubview(gestureNodeBtn)
   btnArray.append(gestureNodeBtn)
  }
 }
 private func touchesChange(touches: Set<UITouch>) {
  //獲取 觸摸對(duì)象 ,觸摸對(duì)象的位置坐標(biāo)來實(shí)現(xiàn)
  gesturePoint = touches.first!.locationInView(self)
  for btn in btnArray {
   //判斷 手指的坐標(biāo) 是否在 button的坐標(biāo)里
   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é)點(diǎn)
    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會(huì)自動(dòng)調(diào)用drawRect方法 進(jìn)行畫線
  self.setNeedsDisplay()
 }
 //計(jì)算2個(gè)節(jié)點(diǎn)中心的坐標(biāo)
 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坐標(biāo): 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方法中進(jìn)行畫線。

 override func drawRect(rect: CGRect) {
  print("執(zhí)行drawRect")
  let context = UIGraphicsGetCurrentContext() //獲取畫筆上下文
  var i = 0
  for tag in selectBtnTagArray {
   if (0 == i) {
    //開始畫線,設(shè)置直線的起點(diǎn)坐標(biāo)
    CGContextMoveToPoint(context, btnArray[tag].center.x, btnArray[tag].center.y)
   } else {
    //畫直線,設(shè)置直線的終點(diǎn)坐標(biāo)
    CGContextAddLineToPoint(context, btnArray[tag].center.x,btnArray[tag].center.y)
   }
   i = i+1
  }
  //如果有選中的節(jié)點(diǎn),就取 跟著 手指的滑動(dòng) 畫線
  if (selectBtnTagArray.count > 0) {
   // 移除最后一條多余的線,
   if gesturePoint != CGPointZero {
    CGContextAddLineToPoint(context, gesturePoint.x, gesturePoint.y)
   }
  }
  CGContextSetLineWidth(context, 10)  //設(shè)置畫筆寬度
  CGContextSetLineJoin(context, .Round) //兩個(gè)線相交點(diǎn) 平滑處理
  CGContextSetLineCap(context, .Round) //設(shè)置線條兩端的樣式為圓角
  CGContextSetRGBStrokeColor(context, 227/255.0, 54/255.0, 58/255.0, 1)
  CGContextStrokePath(context)   // //對(duì)線條進(jìn)行渲染
 }

二、指紋驗(yàn)證

iPhone 的Home鍵 上的金屬環(huán) 能感應(yīng)手指,通知Touch ID 讀取指紋,Touch ID 傳感器 可以拍攝 皮膚 皮下層指紋。

每次使用指紋,Touch ID 會(huì)持續(xù)的添加新的指紋特性,進(jìn)一步提高準(zhǔn)確、安全性。

Touch ID 不會(huì)儲(chǔ)存指紋的圖像。它只存儲(chǔ)指紋的數(shù)學(xué)表達(dá)式。設(shè)備中的芯片還包含稱為“Secure Enclave”的高級(jí)安全架構(gòu),專門用于保護(hù)密碼和指紋數(shù)據(jù)。指紋數(shù)據(jù)通過 Secure Enclave 的專用密鑰得到加密和保護(hù)。iOS 和其他 app 絕不會(huì)訪問您的指紋數(shù)據(jù),指紋數(shù)據(jù)絕不會(huì)存儲(chǔ)到 Apple 服務(wù)器、 iCloud 或其他地方。

1、  import LocalAuthentication

2、用 LAContext 類的  canEvaluatePolicy 方法 判斷設(shè)備是否支持指紋,然后用 evaluatePolicy 方法 來進(jìn)行指紋驗(yàn)證。

執(zhí)行evaluatePolicy方法,系統(tǒng)會(huì)自動(dòng)彈出 驗(yàn)證指紋的提示框,提示副標(biāo)題可以自己設(shè)定。

如果輸入錯(cuò)誤后,系統(tǒng)會(huì)自動(dòng)進(jìn)入“再試一次”的提示框,點(diǎn)擊右邊的 “輸入密碼”選項(xiàng),要自己寫代碼實(shí)現(xiàn) 輸入密碼的彈框。

Demo地址:https://github.com/bugaoshuni/TouchIDAndGestureLock

以上所述是小編給大家介紹的使用Swift代碼實(shí)現(xiàn)iOS手勢(shì)解鎖、指紋解鎖實(shí)例詳解,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論