This example for you to share the iOS hand-sign password unlock the relevant code for your reference, the specific content as follows
LOCKVIEW.M//gesture unlock////Created by Daniel on 16/4/4. copyright©2016 years Daniel.
All rights reserved. #import "LockView.h" @interface LockView ()/** Save the selected button * * * @property (nonatomic, strong) Nsmutablearray *select
EDBTN;
/** Current Touch Point * * * @property (nonatomic, assign) Cgpoint curp; @end @implementation LockView-(Nsmutablearray *) selectedbtn {if (_selectedbtn = = nil) {_selectedbtn = [Nsmut
Ablearray array];
return _selectedbtn;
}-(Ibaction) Pan: (Uipangesturerecognizer *) Pan {//Get current touch point _curp = [Pan locationinview:self];
To determine if the touch point is not on the button for (UIButton *btn in self.subviews) {//If the button is set to the selected state/the touch point must be selected at the button center point 30, more accurate, experience better
CGRect rect = CGRectMake (btn.center.x, Btn.center.y, 30, 30);
if (Cgrectcontainspoint (rect, _curp) && btn.selected = = NO) {btn.selected = YES;
Save the selected button [Self.selectedbtn addobject:btn]; }//Redraw, Invoke DrawRect method [self setneedSdisplay];
When the finger is raised, if (pan.state = = uigesturerecognizerstateended) {nsmutablestring *STRM = [nsmutablestring string];
If the current touch point is not on the button, it displays the selected button and the line UIButton *lastbtn = [self.selectedbtn lastobject] When the finger is raised;
Set the center point of the last selected button to the current touch point, and remove the last _curp = Lastbtn.center;
For (UIButton *btn in self.selectedbtn) {//Save gesture Password [strm appendformat:@ '%ld ', Btn.tag];
//STRM is the password NSLog (@ "%@", STRM);
TODO: Compare the previously saved password, and if so, jump directly to the interface//nslog (@ "finger lift"); Here it is. The system automatically calls the redraw method after the execution/waits for the 2s to clear all the lines, clears the button's selected state Dispatch_after (Dispatch_time_now, (int64_t) (Dispatch_time) ( 1.2 * nsec_per_sec)), Dispatch_get_main_queue (), ^{//Cancel button selected//[self.selectedbtn makeobjectsperformselector:@s Elector (setselected:) withobject:no]; This sentence does not know why no effect, had to use a loop for (UIButton *btn in self.selectedbtn) {[Btn Setse
Lected:no];
//clear the line, empty the check button [self.selectedbtn removeallobjects]; [Self setneedsdisplay];
});
}-(void) DrawRect: (cgrect) Rect {nsinteger count = Self.selectedBtn.count;
If there is no button selected, do not draw line if (count = = 0) {return;
} Uibezierpath *path = [Uibezierpath Bezierpath];
Connect all the points to the line for (int i = 0; i < count; i++) {UIButton * btn = self.selectedbtn[i];
if (i = = 0) {//If it is the first point, set to start [path MoveToPoint:btn.center];
}else {//Add connection [path AddLineToPoint:btn.center];
}//Draw the last point to the line between the finger touch Point [path addlinetopoint:_curp];
[[Uicolor Greencolor]set];
Path.linejoinstyle = Kcglinejoinround;
Path.linewidth = 8;
[Path stroke]; }-(void) awakefromnib {//Create 9 buttons for (int i = 0; i < 9 i++) {UIButton *btn = [UIButton buttonwithtype:
Uibuttontypecustom];
Do not allow interaction with the user, that is, click events do not handle btn.userinteractionenabled = no;
Btn.tag = i; [Btn setimage:[uiimage imagenamed:@ "Gesture_node_normal"] forstate:uicontrolstateNormal];
[Btn setimage:[uiimage imagenamed:@ "gesture_node_highlighted"] forstate:uicontrolstateselected];
[Self addsubview:btn];
}-(void) layoutsubviews {[Super layoutsubviews];
Layout child control Nsinteger count = Self.subviews.count;
Number of columns int cols = 3;
CGFloat x = 0;
CGFloat y = 0;
CGFloat w = 74;
CGFloat h = 74;
Spacing cgfloat margin = (self.bounds.size.width-w * cols)/(cols + 1);
CGFloat col = 0;
CGFloat row = 0;
for (Nsinteger i = 0; i < count; i++) {UIButton *btn = self.subviews[i];
Calculates the column lines of the current button and the xy value col = i% cols;
row = I/cols;
x = margin + col * (margin + W);
y = row * (margin + W);
Btn.frame = CGRectMake (x, Y, W, h);
}} @end
Effect Chart:
The above is the entire content of this article, I hope to help you learn.