One: To implement a custom view, the code in the. h,.m file is as follows:
#import <UIKit/UIKit.h>@class zllockview; @protocol Zllockviewdelegate <nsobject>-(void) LockView: (Zllockview *) LockView didselectedpwd: (NSString *) PWD; @end @interface IDdelegate; @end
////zllockview.m//Gesture Unlock Demo Implementation////Created by Mac on 16/1/9.//copyright©2016 year Mac. All rights reserved.//#import "ZLLockView.h"@interfaceZllockview () @property (nonatomic, strong) Nsmutablearray*btnsselected; @property (nonatomic, assign) Cgpoint Lastpoint;@end@implementationZllockview-(Nsmutablearray *) btnsselected{if(!_btnsselected) {_btnsselected=[Nsmutablearray array]; } return_btnsselected;}-(instancetype) init{if(self =[Super Init]) {[Self setbtn]; } returnSelf ;}- (void) setbtn{ for(inti =0; I <9; i + +) {UIButton*BTN =[[UIButton alloc] init]; Btn.tag=i; [Btn setimage:[uiimage imagenamed:@"Gesture_node_normal"] Forstate:uicontrolstatenormal]; [Btn setimage:[uiimage imagenamed:@"gesture_node_highlighted"] forstate:uicontrolstateselected]; Btn.userinteractionenabled=NO; [Self addsubview:btn]; }}//Only override Drawrect:if perform custom drawing.//An empty implementation adversely affects performance during animation.- (void) DrawRect: (cgrect) rect {//Drawing CodeUibezierpath *path =[Uibezierpath Bezierpath]; Path.linewidth=7; Path.linecapstyle=Kcglinecapround; Path.linejoinstyle=Kcglinejoinround; [[Uicolor Bluecolor]Set]; Nsinteger Count=Self.btnsSelected.count; if(Count = =0)return; for(Nsinteger i =0; I < count; i + +) {UIButton*BTN =Self.btnsselected[i]; if(i = =0) {[path moveToPoint:btn.center]; }Else{[path addLineToPoint:btn.center]; }} [path AddLineToPoint:self.lastPoint]; [Path stroke];}- (void) layoutsubviews{cgfloat btnw= About; CGFloat BTNH= About;//spacingCGFloat padding = (SELF.FRAME.SIZE.WIDTH-BTNW *3) /4; Nsinteger Count=Self.subviews.count; for(Nsinteger i =0; I < count; i + +) {UIButton*BTN =Self.subviews[i];//the column where the current button is locatedNsinteger column = i%3;//calculates the X-value of a btnCGFloat btnx = (column+1) * padding + column *BTNW;//the row where the current button is locatedCGFloat row = I/3; CGFloat Btny= (row+1) * padding + row *BTNW; Btn.frame=CGRectMake (btnx, Btny, BTNW, BTNH); }}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[Self touchesmoved:touches withevent:Event];}- (void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{//gets the current pointUitouch *touch =[touches anyobject]; Cgpoint Location=[Touch LocationInView:touch.view]; //Judging is not in range for(UIButton *btninchself.subviews) {if(Cgrectcontainspoint (Btn.frame, location)) {//Judging the points obtained are not within the range//Place the selected button in the array if(btn.selected = =NO) {[self.btnsselected addobject:btn]; } [btn Setselected:yes]; } ElseSelf.lastpoint =Location ; } [self setneedsdisplay];}- (void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{nsmutablestring*pwd = [nsmutablestringstring]; for(UIButton *btninchself.btnsselected) {[btn Setselected:no]; //Index of the Stitching check button[PWD AppendFormat:@"%ld", Btn.tag]; [Self setneedsdisplay]; } [self.btnsselected removeallobjects]; NSLog (@"%@", PWD); [Self.Delegatelockview:self didselectedpwd:pwd];}@end
Second: Implement the Proxy method in the controller, the code is as follows:
////VIEWCONTROLLER.M//Gesture Unlock Demo Implementation////Created by Mac on 16/1/9.//copyright©2016 year Mac. All rights reserved.//#import "ViewController.h"#import "ZLLockView.h"#import "mbprogresshud+cz.h"@interfaceViewcontroller () <ZLLockViewDelegate>@property (nonatomic, strong) Zllockview*LockView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor= [Uicolor colorwithpatternimage:[uiimage imagenamed:@"HOME_REFRESH_BG"]]; //additional setup after loading the view, typically from a nib.Zllockview *lockview =[[Zllockview alloc] init]; CGFloat Screenw=[UIScreen mainscreen].bounds.size.width; Lockview.frame= CGRectMake (0,0, Screenw, SCREENW); Lockview.center=Self.view.center; Lockview.backgroundcolor=[Uicolor Clearcolor]; [Self.view Addsubview:lockview]; Self.lockview=LockView; LockView.Delegate=Self ; }- (void) LockView: (Zllockview *) LockView didselectedpwd: (NSString *) pwd{if([pwd isequal:@"012345678"]) {[Mbprogresshud showmessage:@"The password is correct!"]; [Self.lockview Removefromsuperview]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Mbprogresshud hidehud]; }); }Else{[Mbprogresshud showmessage:@"wrong password!"]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Mbprogresshud hidehud]; }); }}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Three: Effect:
The implementation of the 2016-1-10 gesture unlock Demo