Final:
Controller:
beyondviewcontroller.m// 39_ touch unlock//// Created by Beyond on 14-9-17.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" #import "LockView.h" #import "LockViewDelegate.h"// Compliance Agreement, used to learn that the unlocking path the user drew on the LockView @interface Beyondviewcontroller () <LockViewDelegate> @end @implementation Beyondviewcontroller#pragma Mark-lockview Proxy method//used to know that the user draws on the LockView unlock path-(void) LockView: (LockView *) LockView Didfinishpath: (NSString *) path{ [Mbprogresshud Showsuccess:path]; NSLog (@ "Get the user's gesture path:%@", path);} @end <span style= "Font-family:courier new;color: #393939;" ><span style= "FONT-SIZE:24PX; line-height:26px; Background-color:rgb (245, 245, 245); " ><strong></strong></span></span>
Custom Circlebutton
circle.m// 39_ touch unlock//// Created by Beyond on 14-9-17.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import ' Circle.h ' @implementation circle-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) { //custom method, initialize [Auto Setup]; } return self;} -(ID) Initwithcoder: (Nscoder *) adecoder{ if (self = [Super Initwithcoder:adecoder]) { //custom method, Initialize [ Self setup]; } return self;} Custom method, initialize-(void) setup{ //Settings button unavailable (not clickable) self.userinteractionenabled = no; Set the default background picture [self setbackgroundimage:[uiimage imagenamed:@ "Gesture_node_normal"] forstate: UIControlStateNormal]; Set background picture When selected (selected) [self setbackgroundimage:[uiimage imagenamed:@ "gesture_node_highlighted"] forstate: Uicontrolstateselected];} @end
protocol defined by LockView
lockviewdelegate.h// 39_ touch unlock//// Created by Beyond on 14-9-17.// Copyright (c) 2014 Com.beyond. All rights reserved.// custom LockView agents must comply with the protocol #import <Foundation/Foundation.h> @class LockView; @protocol Lockviewdelegate <NSObject> @optional//When the lock screen gesture is finished, call this method to notify the outside-(void) LockView: (LockView *) LockView Didfinishpath: (NSString *) path; @end
core code, custom LockView
lockview.h// 39_ touch unlock//// Created by Beyond on 14-9-17.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @protocol lockviewdelegate; @interface lockview:uiview//Proxy for the current controller , the purpose is, after the unlocking gesture is drawn, notify it @property (nonatomic, weak) Iboutlet id<lockviewdelegate> delegate; @end
lockview.m//39_ Touch Unlock////Created by Beyond on 14-9-17.//Copyright (c) 2014 Com.beyond. All rights reserved.//Focus, Core ~ ~ ~ #import "LockView.h" #import "Circle.h" #import "LockViewDelegate.h" @interface LockView ( )//array, remember all the touchmove passes through the Circle@property (nonatomic, strong) Nsmutablearray *circlearr;//finger has been with the screen to keep touch with the dots, the active dots, The segment used to draw the center of the last circle to the user touch point @property (nonatomic, assign) Cgpoint activepoint; @end @implementation Lockview#pragma Mark -Lazy loading//array, remember all Touchmove passed by circle-(Nsmutablearray *) circlearr{if (_circlearr = = nil) {_circlearr = [nsmutabl Earray array]; } return _circlearr;} #pragma mark-initialize-(ID) initWithFrame: (cgrect) frame{self = [super Initwithframe:frame]; if (self) {//Call a custom method, add all buttons at one time [addallcircles]; } return self;} -(ID) Initwithcoder: (Nscoder *) adecoder{if (self = [Super Initwithcoder:adecoder]) {//Call custom method, add all buttons at once [Self addallcircles]; } return self;} Call a custom method to add all buttons at once-(void) addallcircles{for (int index = 0; index<9; index++) {//Create custom button, bind tag to be used in the future to verify the order of the lock circle *btn = [Circle button Withtype:uibuttontypecustom]; Btn.tag = index; Add button [self addsubview:btn]; }} #pragma mark-parent class method//Each add a circle, adjust all child controls frame-(void) layoutsubviews{//must first call the parent class method [Super Layoutsubviews]; Iterate through all the sub buttons, readjust frame for (int i = 0; i<self.subviews.count; i++) {//remove button Circle *btn = Self.subvi Ews[i]; Set frame cgfloat BTNW = 74; CGFloat BTNH = 74; Total three columns int totalcolumns = 3; The I-buttons are in columns int col = i% Totalcolumns; The row int row = I/totalcolumns of the first button; Spacing evenly cgfloat Marginx = (Self.frame.size.width-totalcolumns * btnw)/(Totalcolumns + 1); CGFloat marginy = Marginx; Set frame CGFloat btnx = Marginx + col * (BTNW + Marginx); CGFloat Btny = row * (BTNH + marginy); Btn.frame = CGRectMake (Btnx, Btny, BTNW, BTNH); }} #pragma mark-touch method//Finger Press-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{//1. You must first clear the last (finger-to-screen contact point ) Self.activepoint = Cgpointzero; 2. Call the custom method to get the touch starting at this point cgpoint startPoint = [self pointwithtouches:touches]; 3. Call the custom method to get the touch button (if any) Circle *btn = [self buttonwithpoint:startpoint]; 4. Set the status of the button to be touched (if any) if (btn && btn.selected = = NO) {btn.selected = YES; The button array does not have to be added to the object array [Self.circlearr ADDOBJECT:BTN]; }//5. Refresh the interface, the following method will automatically call DrawRect [self setneedsdisplay];} Finger movement (very high call frequency)-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{//1. Call the custom method to get the point in the touch process Cgpoint POS = [self pointwithtouches:touches]; 2. Call the custom method to get the button (if any) that touches the touch when moving, Circle *btn = [self buttonwithpoint:pos]; 3. Set the state (if any) of the button that is touched when the touch is moved (btn && btn.selected = = NO) {//Touch the button btn.selected = YES; Not in the button array to add to the object array [Self.circlearr ADDOBJECT:BTN]; } else {//does not touch the button, it updates the position of the activepoint so that drawrect self.activepoint = pos; }//4. Refresh the interface, the following method will automatically call DrawRect [self setneedsdisplay];} Touch end (hand lifted), notification agent,-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{//1. Notify the agent if ([Self.delegate re Spondstoselector: @selector (Lockview:didfinishpath:)]) {nsmutablestring *path = [nsmutablestring string]; For (Circle *btn in Self.circlearr) {//tag at this time, play a key role [path appendformat:@ "%d", Btn.tag]; } [Self.delegate lockview:self Didfinishpath:path]; }//2. Optional, clear All button selected state (back to the original) [Self.circlearr makeobjectsperformselector: @selector (setselected:) withobject:@ (NO)]; 3. Clear the selected array of button objects (prepare for next finger press) [Self.circlearr removeallobjects]; 4. Refresh the interface, the following method will automatically call DrawRect [self setneedsdisplay];} Touch interrupted (like end)-(void) touchescancelled: (Nsset *) touches withevent: (uievent *) event{[self touchesended:touches witheve Nt:event];} #pragma Mark-Extracted custom method//custom method, according to touches collection to obtain corresponding touch point position coordinates-(cgpoint) Pointwithtouches: (Nsset *) touches{Uitouch *touch = [touches Anyobject]; Returns the coordinate of the finger touch point in the corresponding view return [touch LocationInView:touch.view];} The custom method, traversing all buttons according to the location coordinates of the touch point, gets the button to which the finger is touched (if any)-(circle *) Buttonwithpoint: (Cgpoint) point{for (Circle *btn in Self.subviews ) {//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)) {if (Cgrectcontainspoint, Point) {return btn; }} return nil;} #pragma mark-Core ~ drawing//Last step-(void) DrawRect: (cgrect) rect{//If no button is encountered, direct return without drawing line if (Self.circleArr.count = = 0) ret Urn 1. Start a Bezier path uibezierpath *path = [Uibezierpath Bezierpath]; 2. Loop through all the buttons to add the center of the button you hit to the path for (int i = 0; i<self.circlearr.count; i++) {Circle *btn = Self.circlearR[i]; if (i = = 0) {//set path starting point [path MoveToPoint:btn.center]; } else {//Add to Path [path AddLineToPoint:btn.center]; }}//3. Connect the last one, that is, the last Hit button center to ActivePoint if (Cgpointequaltopoint (self.activepoint, cgpointzero) = = NO) { Must be detected first [path AddLineToPoint:self.activePoint]; }//4. Set drawing Properties Path.linewidth = 8; Round joint path.linejoinstyle = Kcglinejoinbevel; Line Blue [[Uicolor colorwithred:32/255.0 green:210/255.0 blue:254/255.0 alpha:0.5] set]; 5. Close the Bezier path [path stroke];} @end
Storyboard
Ios_39_ Touch Unlock