iOS gesture unlock

Source: Internet
Author: User

Gesture to unlock, this little thing looked very tall before, but in fact the content is not particularly difficult, understand it is not abstruse, thinking of the rationale


1, the controller put a imageview background picture, drag a view to put in the middle of the screen to put 9 buttons;

2. Customize view bindings in view class and controller, all content is written in custom view;

3, Package a method, add 9 buttons, and set the normal and select the state of the picture and the button's tag value, convenient to finally take the path, and let the button can not interact with the user, because to the overall control of the position of the finger, can not use the button click event;

4, the method that is called when parsing Xib-(ID) Initwithcoder: (Nscoder *) Sdecoder, or the method called when loading Xib is completed-(void) awakefromnib The method of calling Add button is OK;

5, set the position of the button, need in-(void) Layoutsubviews This method set, to use the nine lattice algorithm;

6, the settings after the button can be displayed, the next to deal with gestures, first in the hand pointing at the time to determine whether the current point is not on the button, if the button above to be added to the pre-prepared button array, and the state of the button is not selected;

7, Next is when the finger moves, the same time as the finger start point, but to call the Setneedsdisplay method at the end, but to the prepared current position assignment, to redraw

8. Finally, when the finger is lifted, the tag value of the splicing button forms the path, and the Setseleed method of the button is called so that the button is not selected, and all values in the button array are emptied and redrawn.

9, the most important time to draw the method: the button array to connect the button, and then connect the current position prepared in advance, set their favorite line style, the final rendering can be.


Code:

cflockview.m//gesture unlock////Created by Chenfei on 15/6/19.//Copyright (c) 2015 Chenfei. All rights reserved.//#import "CFLockView.h" @interface Cflockview () @property (nonatomic, strong) Nsmutablearray *btns; @property (nonatomic, assign) Cgpoint Movep; @end @implementation cflockview//Lazy Loading-(Nsmutablearray *) btns{if (_btns = = ni    L) {_btns = [Nsmutablearray array]; } return _btns;}    When loading Xib completes, call-(void) awakefromnib{}-(void) DrawRect: (cgrect) Rect {if (!self.btns.count) return; Uibezierpath *path = [Uibezierpath bezierpath];//takes the first as a starting point for (int i = 0; i < Self.btns.count; i++) {UIB        Utton *btn = self.btns[i];        if (i = = 0) {[path moveToPoint:btn.center];        }else{[path AddLineToPoint:btn.center];    }}//Connect the position of the current finger [path AddLineToPoint:self.moveP];    [[Uicolor Greencolor] set];    Path.linewidth = 8; Path.linejoinstyle = kcglinecapround;//render [path stroke];} Called when parsing Xib-(ID) initwithcodER: (nscoder *) adecoder{if (self = [Super Initwithcoder:adecoder]) {[Self addbtns]; } return self;} -(void) addbtns{for (int i = 0; i < 9; i++) {UIButton *button = [UIButton buttonwithtype:uibuttontypecustom        ];        Button.tag = i + 1;                [Button setimage:[uiimage imagenamed:@ "Gesture_node_normal"] forstate:uicontrolstatenormal];                [Button setimage:[uiimage imagenamed:@ "gesture_node_highlighted"] forstate:uicontrolstateselected];                button.userinteractionenabled = NO;    [Self Addsubview:button];        }}//set button frame-(void) layoutsubviews{[Super Layoutsubviews];    CGFloat col = 0;        CGFloat row = 0;    CGFloat BTNW = 74;    CGFloat BTNH = 74;    CGFloat btnx = 0;        CGFloat btny = 0;    CGFloat totcol = 3;        CGFloat margin = (Self.bounds.size.width-totcol * btnw)/(Totcol + 1);        for (int i = 0; i < Self.subviews.count; i++) {UIButton *button = self.subviews[i]; Col = i % 3;        row = I/3;        Btnx = margin + (margin + btnw) * COL;        Btny = margin + (margin + btnh) * ROW;    Button.frame = CGRectMake (Btnx, Btny, BTNW, BTNH);    }}-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{Uitouch *touch = [touches anyobject];        Cgpoint pos = [Touch locationinview:self];            For (UIButton *btn in self.subviews) {if (Cgrectcontainspoint (Btn.frame, pos) && btn.selected = = NO) {            btn.selected = YES;        [Self.btns ADDOBJECT:BTN];    }}}-(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{Uitouch *touch = [touches anyobject];    Cgpoint pos = [Touch locationinview:self];    SELF.MOVEP = pos;        Modify the small bug, let the finger move to the center of the button to draw,//only to move to the middle of the button 30 * 30 position only Draw Note: button is the cgfloat wh = 30;        For (UIButton *btn in self.subviews) {cgfloat x = btn.center.x-wh * 0.5;        CGFloat y = btn.center.y-wh * 0.5;         CGRect frame = CGRectMake (x, y, WH, WH);       if (Cgrectcontainspoint (frame, pos) && btn.selected = = NO) {btn.selected = YES;        [Self.btns ADDOBJECT:BTN]; }} [self Setneedsdisplay];}    -(void) touchesended: (Nsset *) touches withevent: (uievent *) event{nsmutablestring *str = [nsmutablestring string];    For (UIButton *button in self.btns) {[str appendformat:@ '%d ', button.tag];    } NSLog (@ "%@", str);    [Self.btns makeobjectsperformselector: @selector (setselected:) Withobject:no];    [Self.btns removeallobjects]; [Self setneedsdisplay];} @end


iOS gesture unlock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.