A demo you did during the practice, attached to GitHub address: Https://github.com/H0ng/GestureLock
#HHView. h
#import <UIKit/UIKit.h> @class hhview; @protocol hhviewdelegate <NSObject> @optional-(void) ShowView: ( hhview*) ShowView Didfinishedpath: (NSString *) path; @end @interface Hhview:uiview@property (nonatomic, weak) id< Hhviewdelegate> delegate;
@end
#HHView. M
#import "HHView.h" #import "HHCircleButton.h" @interface Hhview () @property (nonatomic, strong) Nsmutablearray * totalpointpaths; @property (nonatomic, assign) cgpoint currentpoint; @end @implementation hhview-(Nsmutablearray *) totalpointpaths{if (_totalpointpaths = = nil) {Nsmutablearray *array = [Nsmutablearray array]; _totalpointpaths = array; } return _totalpointpaths;} /** * This method is called by Code creation */-(instancetype) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:frame]) {[sel F Setup]; } return self;} /** * Created by XIB,SB will call this method */-(ID) Initwithcoder: (Nscoder *) decoder{if (self = [Super Initwithcoder:decoder]) {[SE LF Setup]; } return self;} /** * Create 9 buttons */-(void) setup{for (int i = 0; i < 9; i++) {Hhcirclebutton *button = [Hhcirclebutton butto Nwithtype:uibuttontypecustom]; Button.tag = i; [Self Addsubview:button]; }}/** * 9-button frame */-(void) layoutsubviews{[Super Layoutsubviews]; for (inti = 0; i < Self.subviews.count; i++) {Hhcirclebutton *button = self.subviews[i]; int col = I/3; int row = i% 3; CGFloat buttonw = 74; CGFloat Buttonh = 74; CGFloat leftparading = (SELF.FRAME.SIZE.WIDTH-BUTTONW * 3)/4.0; CGFloat Buttonx = leftparading + (leftparading + buttonw) * ROW; CGFloat buttony = leftparading + (leftparading + buttonh) * COL; Button.frame = CGRectMake (Buttonx, Buttony, Buttonw, Buttonh); }} #pragma mark-return touches point of point-(Cgpoint) pointwithtouches: (Nsset *) touches{Uitouch *touch = [touches anyobject]; Cgpoint point = [Touch locationinview:self]; return point;} #pragma mark-Determine if the contact is within the frame of the button-(Hhcirclebutton *) Buttonwithpoint: (Cgpoint) point{for (Hhcirclebutton *btn in SELF.SUBV iews) {cgfloat wh = 24; CGFloat FrameX = btn.center.x-wh * 0.5; CGFloat Framey = btn.center.y-wh * 0.5; if (Cgrectcontainspoint (CGRectMake (FrameX, Framey, WH, WH),Point) {return btn; }} return nil;} #pragma mark-touchesbegan-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{self.currentpoint = CGPoint Zero; Gets the current touch point cgpoint points = [self pointwithtouches:touches]; Judge if the touch point is on a btn, return this btn, otherwise return nil Hhcirclebutton *btn = [self buttonwithpoint:point]; Determine if the btn, and whether the BTN is selected, if not, select if (btn && btn.selected = no) {btn.selected = YES; [Self.totalpointpaths ADDOBJECT:BTN]; }//Redraw [self setneedsdisplay];} #pragma mark-touchesmoved-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{cgpoint point = [Self point Withtouches:touches]; Hhcirclebutton *btn = [self buttonwithpoint:point]; Determine if the button has been selected if (btn && btn.selected = = NO) {btn.selected = YES; [Self.totalpointpaths ADDOBJECT:BTN]; }else{self.currentpoint = point; } [self setneedsdisplay];} #pragma mark-touchesended-(vOID) touchesended: (Nsset *) touches withevent: (uievent *) event{//cgpoint point = [self pointwithtouches:touches];/// /Hhcirclebutton *btn = [self buttonwithpoint:point];////if (btn && btn.selected = NO) {//BTN. selected = yes;//[self.totalpointpaths addobject:btn];//}else{//self.currentpoint = point;//} if ([Self.delegate respondstoselector: @selector (Showview:didfinishedpath:)]) {nsmutablestring *path = [NSMutabl Estring string]; For (Hhcirclebutton *btn in self.totalpointpaths) {[path appendformat:@ '%ld ', (long) btn.tag]; } [Self.delegate showview:self Didfinishedpath:path]; }//Will current clears 0 self.currentpoint = Cgpointzero; Then set all buttons unchecked//[Self.totalpointpaths Makeobjectsperformselector: @selector (setselected:) withobject:@ (NO)] ;//[Self.totalpointpaths removeallobjects]; [Self setneedsdisplay]; }-(void) touchescancelled: (Nsset *) touches withevent: (UievENT *) event{[self touchesended:touches withevent:event];} #pragma mark-Draw the link between buttons-(void) DrawRect: (CGRect) rect{if (Self.totalpointPaths.count = = 0) return; Uibezierpath *path = [Uibezierpath Bezierpath]; for (int index = 0; index < self.totalpointPaths.count; index++) {Hhcirclebutton *btn = Self.totalpointpaths[ind EX]; if (index = = 0) {[path moveToPoint:btn.center]; }else{[path AddLineToPoint:btn.center]; }} if (Cgpointequaltopoint (self.currentpoint, cgpointzero) = = NO) {[Path AddLineToPoint:self.currentPoi NT]; } path.linewidth = 10; Path.linejoinstyle = Kcglinejoinround; [[Uicolor colorwithred:32/255.0 green:210/255.0 blue:254/255.0 alpha:0.5] set]; [Path stroke];}
#HHCircleButton. h
#import <UIKit/UIKit.h> @interface Hhcirclebutton:uibutton@end
#HHCircleButton. M
#import "HHCircleButton.h" @implementation hhcirclebutton-(ID) Initwithcoder: (Nscoder *) decoder{ if (self = [super Initwithcoder:decoder]) { [self setup]; } return self;} -(Instancetype) initWithFrame: (cgrect) frame{ if (self = [Super Initwithframe:frame]) { [self setup]; } return self;} #pragma mark-Set button state picture-(void) setup{ self.userinteractionenabled = NO; [Self setbackgroundimage:[uiimage imagenamed:@ "Gesture_node_normal"] forstate:uicontrolstatenormal]; [Self setbackgroundimage:[uiimage imagenamed:@ "gesture_node_highlighted"] forstate:uicontrolstateselected];} @end
#ViewController. h
#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller@end
#ViewController. M
#import "ViewController.h" #import "HHView.h" @interface Viewcontroller ()
iOS gesture unlock