Use swift code to implement IOS gesture unlock, fingerprint unlock example detailed _ios

Source: Internet
Author: User

One, gesture password

1,

1.1, using UIButton to form the node of the gesture.

1.2. When the finger touches the screen, call the overridden Touchesbegan:withevent method (call Setneedsdisplay in Touchesbegan, which automatically invokes the DrawRect method).

1.3. When the finger slides on the screen, invoke the overridden Touchesended:withevent method.

The two methods perform the same operation: get the coordinates of the touch through the Locationinview, and then use Cgrectcontainspoint to determine whether the finger passes through the UIButton, and if you go through the button, replace the picture of the button, and save the tag with the button.

1.4, by default, jump line 1th and 3rd nodes, the middle of the 2nd node will be ignored, so to separate processing. Calculates the coordinates between the 1th and 3rd nodes based on the coordinates of the 2 UIButton of the 1 and 3 nodes, determines whether the coordinates exist UIButton, and if so, joins the settings to select and adds the selected array.

Here we have achieved the effect of the node being selected when the finger is sliding over the node:

 MARK:-Override//When the finger touches the screen, the Touchesbegan:withevent method is invoked; Override func Touchesbegan (TOUCHES:SET&LT;UITOUCH&GT; Withevent event:uievent?) {print ("Execute Touchesbegan") Selectbtntagarray.removeall () Toucheschange (touches)}//When the finger moves on the screen, call Touchesmoved:withe Vent method; override Func touchesmoved (touches:set<uitouch>, withevent event:uievent?)
 {Toucheschange (touches)}//When the touch is canceled (for example, when a call is interrupted during a touch), the Touchescancelled:withevent method is invoked. Override Func touchescancelled (Touches:set<uitouch>?, Withevent event:uievent?) {}//When the finger leaves the screen, the Touchesended:withevent method is invoked; override Func touchesended (touches:set<uitouch>, Withevent event:u IEvent?) {print ("execute touchesended") var alerttitle = "Please set the correct gesture" var alertmessage = "Gesture password cannot be less than 4" var issuccess = False if s Electbtntagarray.count >= 4 {alerttitle = "gesture Password Set success" Issuccess = True alertmessage = "Password: \ (selectbtntagarray ) "} gesturelockdelegate!. Gesturelocksuccess (issuccess, Title:alerttitle, MessagE:alertmessage) Gesturepoint = Cgpointzero;  Self.setneedsdisplay ()}//MARK:-Privatemethod private func initbuttons () {for i in 0...8 {//The first few lines let row = I/3 Let loc = i% 3//Two button spacing let btnspace = (SCREENWIDTH-3*BTNWH)/4 Let btnx = Btnspace + (BTNWH + b Tnspace) * CGFloat (loc) Let Btny = m + btnspace + (BTNWH + btnspace) * cgfloat (row) Let gesturenodebtn = UIButton (f Rame:cgrectmake (Btnx, Btny, BTNWH, btnwh)) Gesturenodebtn.tag = i gesturenodebtn.userinteractionenabled = false//no sound The user's interaction should be. Be sure to add this sentence gesturenodebtn.setimage (uiimage named:btnimgnormal), forstate:. Normal) Self.addsubview (gesturenodebtn) btnarray.append (GESTURENODEBTN)} private func Toucheschange (touches: set<uitouch>) {//Get Touch object, touch object's position coordinates to achieve gesturepoint = touches.first!. Locationinview (self) for btn in Btnarray {//determine if the coordinates of the finger are in the button's coordinates if!selectbtntagarray.contains (Btn.tag) &
   & Cgrectcontainspoint (Btn.frame, Gesturepoint) { Processing Jump link var linecenterpoint:cgpoint = cgpoint () If Selectbtntagarray.count > 0 {linecenterpoint = Cente Rpoint (Btn.frame.origin, endpoint:btnarray[selectbtntagarray.last!). Frame.origin)}//Save middle skipped node for btn in Btnarray {if!selectbtntagarray.contains (btn.tag) && CG Rectcontainspoint (Btn.frame, Linecenterpoint) {btn.setimage (UIImage (named:btnimgselected), ForState:. Normal) Selectbtntagarray.append (Btn.tag)}}///Save the Tag selectbtntagarray.append (Btn.tag) B of the button that is across Tn.setimage (UIImage (named:btnimgselected), forstate:. Normal)}//setneedsdisplay automatically calls the DrawRect method to draw a line self.setneedsdisplay ()}//compute the coordinates of the center of 2 nodes private func Centerpo Int (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 Coordinates: Leftpoint + (rightpoint-leftpoint)/2 = (rightpoint+leftpoint)/2 return CGPointMake (ri
 Ghtpoint + leftpoint)/2 + BTNWH/2, (Toppoint + bottompoint)/2 + BTNWH/2); } func Recovernodestatus () {Selectbtntagarray.removeall () for btn in Btnarray {btn.setimage (uiimage Gnormal), Forstate:. Normal)} self.setneedsdisplay ()}

2, drawing lines: Draw lines in the DrawRect method.

 override func DrawRect (rect:cgrect) {print ("execute drawrect") Let context = Uigraphics Getcurrentcontext ()//Get brush context var i = 0 for tag in Selectbtntagarray {if (0 = i) {//Start drawing line, set beginning coordinates of line Cgcon Textmovetopoint (context, btnarray[tag].center.x, btnarray[tag].center.y)} else {//Draw a line, set the endpoint coordinates of the line CGCONTEXTADDL Inetopoint (context, btnarray[tag].center.x,btnarray[tag].center.y)} i = i+1}//If there is a selected node, follow the slide line of the finger if (s Electbtntagarray.count > 0) {//Remove the last redundant line, if Gesturepoint!= Cgpointzero {cgcontextaddlinetopoint (context , Gesturepoint.x, Gesturepoint.y}} cgcontextsetlinewidth (context, 10)//Set Brush width cgcontextsetlinejoin (context, . Round)///Two line intersection point smoothing cgcontextsetlinecap (context,. Round)//Set the style at both ends of the line as rounded corner cgcontextsetrgbstrokecolor (context, 227/255.0, 54/255.0, 58/255.0, 1) cgcontextstrokepath (conte XT)////Line render} 

Second, fingerprint verification

The metal ring on the home button of the iPhone can sense the finger, notifies the Touch ID to read the fingerprint, and the Touch ID sensor can photograph the skin's underlying fingerprint.

Each time using a fingerprint, touch ID will continue to add new fingerprint characteristics, further improve the accuracy and security.

The touch ID does not store the fingerprint image. It only stores the mathematical expression of the fingerprint. The chips in the device also contain an advanced security architecture called Secure Enclave, designed to protect passwords and fingerprint data. Fingerprint data is encrypted and protected by secure Enclave private key. IOS and other apps will never access your fingerprint data, and the fingerprint data will never be stored on Apple servers, ICloud, or anywhere else.

1, Import localauthentication

2, using the Lacontext class of Canevaluatepolicy method to determine whether the device supports fingerprints, and then use the EvaluatePolicy method for fingerprint verification.

To perform the EvaluatePolicy method, the system automatically pops up a prompt to verify the fingerprint, suggesting that the subtitle can be set itself.

If the input error, the system will automatically enter the "Try again" prompt box, click on the right "Enter the password" option, to write their own code to achieve the input password of the bomb box.

Demo Address: Https://github.com/bugaoshuni/TouchIDAndGestureLock

The above is a small set to introduce you to the use of Swift code to unlock the iOS hand, fingerprint unlock examples, I hope to help!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.