Animation implementation unlocked-file directory
ViewController.h file
Create custom view, follow protocols, set up proxies, implement proxy methods
1 #import "LYPaintView.h"2 3 #import "ViewController.h"4 5 @interfaceViewcontroller () <LYPaintViewDelegate>6 7 @end8 9 @implementationViewcontrollerTen One- (void) Viewdidload { A [Super Viewdidload]; -Self.view.backgroundcolor=[uicolor colorwithpatternimage:[uiimage imagenamed:@"BG"]]; - //load sub-fixed view theLypaintview *paintview=[Lypaintview Paintview]; - //Set up proxy -Paintview.Delegate=Self ; - +Paintview.center =Self.view.center; - //Add View + [Self.view Addsubview:paintview]; A } at //to modify the state of the status bar --(uistatusbarstyle) preferredstatusbarstyle{ - returnuistatusbarstylelightcontent; - } - //Implementing Proxy Methods --(BOOL) Checkpwd: (Lypaintview *) Paintview withpwd: (NSString *) pawd{ in if([Pawd isequaltostring:@"345"]) { - returnYES; to } + returnNO; - } the @end
LYPaintView.h
File content, primarily creating protocols, setting proxy properties
1 #import<UIKit/UIKit.h>2 @classLypaintview;3 //Create an agreement4 @protocolLypaintviewdelegate <NSObject>5 //method of the Protocol6-(BOOL) Checkpwd: (Lypaintview *) Paintview withpwd: (NSString *) Pawd;7 8 @end9 Ten @interfaceLypaintview:uiview One+(instancetype) Paintview; A@property (Nonatomic,weak)ID<LYPaintViewDelegate>Delegate; - @end
lypaintview.m
Implement unlock
#import "LYPaintView.h"#defineCzlinecolor [Uicolor colorwithred:0.0 green:170/255.0 blue:255/255.0 alpha:0.5]@interfaceLypaintview () @property (nonatomic,strong) Nsmutablearray*Btnsarray, @property (nonatomic,strong) Uicolor*LineColor; @property (nonatomic,assign) cgpoint currentpoint;@end@implementationLypaintview//quickly create object class methods+(instancetype) paintview{return[[[NSBundle mainbundle]loadnibnamed:@"Lypaintview"Owner:nil Options:nil]lastobject]; }//Start Touch Event-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //Get Touch PointUitouch *touch=Touches.anyobject; Cgpoint Point=[Touch locationinview:self]; //traverse the button to see if the touch point is on the button[Self.subviews enumerateobjectsusingblock:^ (__kindof UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {BOOL Iscontain=Cgrectcontainspoint (Obj.frame, point); if(Iscontain && obj.highlighted==NO) {obj.highlighted=YES; [Self.btnsarray Addobject:obj]; }Else{obj.highlighted=NO; } }]; }//Touch When moving-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //gets the current touch pointUitouch *touch=Touches.anyobject; Cgpoint Point=[Touch locationinview:self]; //set the current pointSelf.currentpoint=Point ; //traverse the button to see if the touch point is on the button[Self.subviews enumerateobjectsusingblock:^ (__kindof UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {BOOL Iscontain=Cgrectcontainspoint (Obj.frame, point); if(Iscontain && obj.highlighted==NO) {obj.highlighted=YES; [Self.btnsarray Addobject:obj]; } }]; //Redraw[self setneedsdisplay];}//at the end of touch-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //eliminate the last lineUIButton *lastbtn =Self.btnsArray.lastObject; Self.currentpoint=Lastbtn.center; [Self setneedsdisplay]; //traverse the touch button to get the password enteredNsmutablestring *password=[nsmutablestringstring]; [Self.btnsarray Enumerateobjectsusingblock:^ (UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {Nsinteger tag=Obj.tag; [Password AppendFormat:@"%@", @ (tag)]; }]; BOOL isTrue; //The proxy implementation determines whether the password is consistent if([Self.Delegaterespondstoselector: @selector (checkpwd:withpwd:)]) {IsTrue= [Self.Delegatecheckpwd:self Withpwd:password]; } if(isTrue) {NSLog (@"Password is correct"); //Restore the state of the button when the password is correct[Self.btnsarray enumerateobjectsusingblock:^ (UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {obj.highlighted=NO; }]; [Self.btnsarray removeallobjects]; [Self setneedsdisplay]; }Else{NSLog (@"Password Error"); //The line turns red when the password is incorrectSelf.userinteractionenabled=NO; [Self.btnsarray Enumerateobjectsusingblock:^ (UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {obj.highlighted=NO; Obj.enabled=NO; }]; Self.linecolor=[Uicolor Redcolor]; [Self setneedsdisplay]; //Delay 1 seconds Restore Button's original stateDispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Self.btnsarray enumerateobjectsusingblock:^ (UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {obj.enabled=YES; }]; [Self.btnsarray removeallobjects]; [Self setneedsdisplay]; Self.userinteractionenabled=YES; Self.linecolor=Czlinecolor; }); } }//Load Button-(void) awakefromnib{Self.linecolor=Czlinecolor; for(intI=0; i<9; i++) { //Create a button and addUIButton *btn=[[UIButton alloc]init]; Btn.tag=i; Btn.userinteractionenabled=NO; [Btn setbackgroundimage:[uiimage imagenamed:@"Gesture_node_normal"] Forstate:uicontrolstatenormal]; [Btn setbackgroundimage:[uiimage imagenamed:@"gesture_node_highlighted"] forstate:uicontrolstatehighlighted]; [Btn setbackgroundimage:[uiimage imagenamed:@"Gesture_node_error"] forstate:uicontrolstatedisabled]; [Self addsubview:btn]; } }//set the frame of the button-(void) layoutsubviews{[Super Layoutsubviews]; intconnum=3; CGFloat BTNW= -; CGFloat BTNH= -; CGFloat Magin= (Self.frame.size.width-connum *btnw)/(Connum-1); [Self.subviews Enumerateobjectsusingblock:^ (__kindof UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {Nsinteger col=idx%Connum; Nsinteger Row=IDX/Connum; CGFloat btnx=col * (Magin +BTNW); CGFloat Btny=row * (Magin +BTNH); Obj.frame=CGRectMake (btnx, Btny, BTNW, BTNH); }]; }//DrawRect method Drawing lines- (void) DrawRect: (cgrect) rect {//If there are no elements in the array, do not draw if(self.btnsarray.count==0) { return ; } //Create PathUibezierpath *path=[Uibezierpath Bezierpath]; [Self.btnsarray Enumerateobjectsusingblock:^ (UIButton * _nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) { if(idx==0) {[path moveToPoint:obj.center]; }Else{[path addLineToPoint:obj.center]; } }]; //Add the last segment[path AddLineToPoint:self.currentPoint]; [Self.linecolor Setstroke]; [Path Setlinewidth:Ten]; [Path Setlinecapstyle:kcglinecapround]; [Path Setlinejoinstyle:kcglinejoinround]; //Rendering[path stroke]; }//lazy Load button array-(Nsmutablearray *) btnsarray{if(_btnsarray==Nil) {_btnsarray=[Nsmutablearray array]; } return_btnsarray;}@end
"Code note" gesture unlock