iOS development-Advanced ui-gesture Unlock

Source: Internet
Author: User

First, gesture unlock analysis
1. Build the interface (a full screen uiview and a smaller view)
2. Create a new class Njlockview, set the class of the smaller view to Njlockview
In the NJLOCKVIEW.M

When the view is created from code, it calls initWithFrame: Method
-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Initialization Cade
[Self setup];

}
return self;
}

When a view is created from a xib or a storyboard, it is called
-(ID) Initwithcoder: (Nscoder *) Adecoder
{
if (self = [Super Initwithcoder:adecoder]) {
Create a 9 button
[Self setup];
}
return self;
}

Create 9 buttons to add to a custom view
-(void) setup
{
for (int i = 0;i<9;i++) {
1. Create a button
UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom];
}
2. Set the background image of the button
[Btn setbackgroundimage:[uiimage imagenamed:@ "Gesture_node_normal"] forstate:uicontroolstatenormal];

[Btn setbackgroundimage:[uiimage imagenamed:@ "gesture_node_highlighted"] forstate:uicontroolstateseclected];

3. Add a button to the view
[Self addobejct:btn];


4. Disable button click events (because we need to listen for touch events)
btn.userinteractionenabled = NO;

5. Set the tag of the button as a unique identifier
Btn.tag = i;

}

-(void) layoutsubviews
{
[Super layoutsubviews];//NOTE!!! Be sure to set it, or the consequences are self-esteem
Set the frame of the button
for (int i = 0; i<self.subviews.count;i++) {
1. Remove the corresponding position button
UIButton *btn = self.subviews[1];

2. Set the frame
CGFloat BTNW = 74;
CGFloat BTNH = 74;
2.1. Calculate Spacing
CGFloat margin = (Self.frame.size.width-(3 * BTNW))/4
int col = i% 3;//column number
int row = i/3;//line number
Spacing + column number (or line number) * (Width of button + spacing)
CGFloat btnx = margin + col* (btnw+margin);
CGFloat Btny = margin + col* (btnw+margin);
BTN frame = CGRectMake (BTNX,BTNY,BTNW,BTNH)
}

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
1. Get the point pressed
Cgpoint startPoint = [self getcurrenttouchpoint:touches];
2. Get the Touch button
UIButton *btn = [self getcurrentbtnwithpoint:startpoint];
Storage button
if (BTN)
{
Set selected state
btn.selected = YES;
Save the button in an array
[Self.buttons ADDOBJECT:BTN];
}
}

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{

1. Get the point pressed
Cgpoint movepoint = [self getcurrenttouchpoint:touches];
2. Get the Touch button
UIButton *btn = [self getcurrentbtnwithpoint:movepoint];
Storage button
if (btn && btn.selected!) = YES)
{
Set selected state
btn.selected = YES;
Save the button in an array
[Self.buttons ADDOBJECT:BTN];
}

Record the position of the current finger movement
Self.currentpoint = Movepoint;
Notification View Draw Segment
[Self setneedsdisplay];
}

-(void) touchesended: (Nsset *) touches withevent: (Uievent *) event
{
Remove user-entered password
nsmutablestring *result = [nsmutablestring string];
For (UIButton *btn in self.buttons) {
Result appendformat:@ "%d", Btn.tag];
}
Notifies the agent of the password entered by the delegate user
if (self.delegate respondstoselector: @selector
(lockviewdidclick:andpwd:)]) {
[Self.delegate lockviewdidclick:self Andpwd:result];
}
Restore the highlighted state of the button
[Self.buttons makeobjectsperformselector: @selector (setselected:)
withobject:@ (NO)];
Empty array
[Self.buttons removeallobjects];
[Self setneedsdisplay];
Empty Currentpoint
Self.currentpoint = Cgpointzero;

}

Encapsulates the "Get pressed point" feature.
Gets the point of the current touch based on the Uitouch collection passed into the system
-(Cgpoint) Getcurrenttouchpoint: (Nsset *) touches
{
Uitouch *touch = [touches anyobject];
Cgpoint point = [Touch LocationInView:touch.view];
return point;
}

Encapsulates the function "determine if the touch is within the range of the button".
Get the touch button according to the touch point
-(UIButton *) Getcurrentbtnwithpoint: (cgpoint) point
{
For (UIButton *btn in self.subviews) {
If Cgrectcontainspoint (btn.frame,point) {
return BTN;
}
}
return nil;
}

Declare an array in the class extension to hold the button that the user pressed
@property (Nonatomic,strong) Nsmutablearray *buttnons;
Lazy Loading
-(Nsmutablearray *) buttons
{
if (_buttnons = = nil) {
_buttons = [Nsmutablearray array];
}
return _buttons;
}

//As long as the DrawRect: method is overridden, the default background color for view is black.
-(void) DrawRect: (cgrect) rect
{
//Because the default background color will have a cache, so first clear the context
Cgcontextclearrect (ctx,rect);
// Remove all the buttons from the array and connect the midpoint of all the buttons
Note: You cannot use for-in here, because you want to set the start and end points. Need to judge I, direct for-in can not
Judge I
Cgcontextref ctx = Uigraphicsgetcurrentcontext ();
for (int i = 0;i< self.buttons.count;i++) {
//Remove button
UIButton *btn = self.buttons[i];
if (0 = = i) {
Cgcontextmovetopoint (CTX, BTN.CENTER.X,BTN.CENTER.Y)
} else
{
Cgcontextaddlinetopoint (CTX,BTN.CENTER.X,BTN.CENTER.Y);
};
}
}
if (Self.buttons.count!) = 0) {
//must determine if there are no buttons, if not, then set the end point is meaningless, will error.
Cgcontextaddlinetopoint (CTX,SELF.CURRENTPOINT.X,SELF.CURRENTPOINT.Y);
}
[Uicolor colorwithred:18/ 255.0 green:102/255.0 blue:72/255.0 alpha:0.5] set];
Cgcontextsetlinewidth (ctx,10);
Cgcontextsetlinejoin (CTX , kcglinejoinround);
Cgcontextsetlinecap (Ctx,kcglinecapround);
Cgcontextstrokepath (CTX);
}

A new Cgpoint attribute is added to the class extension (records the position of the user's current finger [not in the button range])
@property (nonatomic,assign) cgpoint currentpoint;

3. Create a protocol and a property in NJLockView.h
@class Njlockview
@protocol njlockviewdelegata<nsobject>
-(void) Lockviewdidclick: (Njlockview *) LockView andpwd: (NSSTIRNG *) pwd;
@end

@interfaceNJLockView: UIView

@property (nonatomic,weak) Iboutlet id<njlockviewdelegate> delegate;

4. Follow the protocol and implement the method in the controller implementation file
@interface Njviewcontroller () <NJLockViewDelegate>

-(void) Lockviewdidclick: (Njlockview *) LockView andpwd: (NSSTIRNG *) pwd
{
NSLog (@ "Mjviewcontroller%@", pwd);
}

iOS development-Advanced ui-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.