Gesture unlocking in iOS development and ios gesture unlocking

Source: Internet
Author: User
Tags button attributes

Gesture unlocking in iOS development and ios gesture unlocking
This article describes how to use Gesture Recognition to unlock a mobile phone. This method is widely used in mobile phone unlocking, password verification, quick payment, and other functions. The example is as follows.

First, we need to analyze the implementation process of the function. First, we need to first look at the general implementation process:

1. Load the jiugongge page

2. Implement button status change during Button clicking and sliding

3. Achieve connections during the Sliding Process

4. Determine whether the password is correct after drawing,

5. jump after the password is determined.

Next we will use the code to implement the above five processes.

1. Load the interface

1.1 The distribution of controls in the jiugongge is 3*3. You can customize the view (including 3*3 buttons) and add them to the viewController.

// Add view neutron control-(void) awakeFromNib {// create button for (int I = 0; I <9; I ++) {self. lineColor = [UIColor blueColor]; UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn. userInteractionEnabled = NO; // set the button attributes [btn setBackgroundImage: [UIImage imageNamed: @ "gesture_node_normal"] forState: UIControlStateNormal]; [btn success: [UIImage imageNamed: @ "unknown"] forState: Unknown]; [btn setBackgroundImage: [UIImage imageNamed: @ "gesture_node_error"] forState: UIControlStateDisabled]; [self addSubview: btn];} // Layout view subcontrol-(void) layoutSubviews {[super layoutSubviews]; CGFloat width = 74; CGFloat height = 74; CGFloat Margin = (self. bounds. size. width-3 * width)/2; // traverses the frame [self. subviews enumerateObjectsUsingBlock: ^ (_ kindof UIView * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {// index marker obj of the tag setting button. tag = idx; int row = (int) idx/3; int col = idx % 3; obj. frame = CGRectMake (col * (Margin + width), row * (Margin + height), width, height) ;}];}

 

1.2 Add the defined view to viewController through xib.

First, define a blockview class method,

// Load the xib file + (instancetype) lockView {return [[[NSBundle mainBundle] loadNibNamed: @ "MYblockView" owner: nil options: nil] lastObject];}

 

Then load it to the Controller.

// Set the background image self of the controller view. view. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "bg"]; MYblockView * blockView = [MYblockView lockView]; blockView. center = self. view. center; // Add blockview to viewController [self. view addSubview: blockView];

 

2. Implement button status change during Button clicking and sliding

2.1 define the member attributes of the array type to install the clicked button

@ Property (nonatomic, strong) NSMutableArray * btnArr; // lazy loading-(NSMutableArray *) btnArr {if (_ btnArr = nil) {_ btnArr = [jsonarray];} return _ btnArr ;}

2.2 create a path and draw a graph

# Pragma mark ---- drawing a graph-(void) drawRect :( CGRect) rect {if (self. btnArr. count = 0) {return;} // create path UIBezierPath * path = [UIBezierPath bezierPath]; // traverse all buttons to draw [self. btnArr enumerateObjectsUsingBlock: ^ (_ kindof UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {// The first button. The center point is the start point if (idx = 0) {[path moveToPoint: obj. center];} else {[path addLineToPoint: obj. center] ;}}]; [path addLineToPoint: self. currentPoint]; // set the path attribute path. lineWidth = 10; path. lineCapStyle = kCGLineCapRound; path. lineJoinStyle = kCGLineJoinRound; [self. lineColor setStroke]; // render [path stroke];}

 

Start to touch 2.3

# Pragma mark ----- start touch-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// obtain the touch object UITouch * touch = touches. anyObject; // obtain the touch point CGPoint loc = [touch locationInView: self]; // retrieve the button to determine whether the touch point is on the button [self. subviews enumerateObjectsUsingBlock: ^ (_ kindof UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {BOOL isContains = CGRectContainsPoint (obj. frame, loc); // if the button is on, save the current button in the array and change the button status if (isContains & obj. highlighted = NO) {[self. btnArr addObject: obj]; obj. highlighted = YES;} else {obj. highlighted = NO ;}}];}

2.4 re-painting during sliding

# Pragma mark ---- start to slide-(void) touchesMoved :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// obtain the touch object UITouch * touch = touches. anyObject; // obtain the touch point CGPoint loc = [touch locationInView: self]; self. currentPoint = loc; // The traversal button. If the button is on the slide path, the button status is changed. subviews enumerateObjectsUsingBlock: ^ (_ kindof UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {BOOL isContains = CGRectContainsPoint (obj. frame, loc); if (isContains & obj. highlighted = NO) {[self. btnArr addObject: obj]; obj. highlighted = YES ;}}]; // re-paint [self setNeedsDisplay];}

3. Achieve the link and 4. Determine whether the password is correct after drawing,

# Pragma mark ---- stop sliding end-(void) touchesEnded :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// define the last button UIButton * lastBtn = [self. btnArr lastObject]; // defines the center of the last button as the current slide Point self. currentPoint = lastBtn. center; // re-paint [self setNeedsDisplay]; // determine the password self. password = [NSMutableString string]; [self. btnArr enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {[self. password appendFormat: @ "% @", @ (obj. tag)] ;}]; NSLog (@ "% @", self. password); BOOL isOk; if ([self. delegate respondsToSelector: @ selector (blockView: finishedWithPassword :)]) {isOk = [self. delegate blockView: self finishedWithPassword: self. password];} if (isOk) {[self. btnArr enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {obj. highlighted = NO;}]; [self. btnArr removeAllObjects]; [self setNeedsDisplay]; NSLog (@ "correct password");} else {NSLog (@ "Incorrect password ");}}

 

Note: During the password determination process, we concatenate strings Based on the button tag value defined in the previous layout button, and pass the password value through a proxy.

# Import <UIKit/UIKit. h> @ class MYblockView; // declare proxy @ protocol MYblockViewDelegate <NSObject> @ optional // proxy method-(BOOL) blockView :( MYblockView *) blockView finishedWithPassword :( NSString *) password; @ end @ interface MYblockView: UIView + (instancetype) lockView; // sets the proxy member attribute @ property (nonatomic, weak) id <MYblockViewDelegate> delegate; @ end

 

5. jump after the password is determined.

Else {// Disable User Interaction self. userInteractionEnabled = NO; [self. btnArr enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {self. lineColor = [UIColor redColor]; obj. highlighted = NO; obj. enabled = NO; [self setNeedsDisplay]; dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1.0 * NSEC_PER_SEC), reset (), ^ {// reply button status [self. btnArr enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {obj. enabled = YES;}]; // restores the line color self. lineColor = [UIColor blueColor]; [self. btnArr removeAllObjects]; [self setNeedsDisplay];}) ;}]; NSLog (@ "Incorrect password");} self. userInteractionEnabled = YES ;}

The proxy determines the password and performs redirection.

-(BOOL)blockView:(MYblockView *)blockView finishedWithPassword:(NSString *)password{    if ([password isEqualToString:@"012"]) {                UIViewController *two=[UIViewController  new];        two.view.backgroundColor=[UIColor greenColor];        [self.navigationController pushViewController:two animated:YES];        return  YES;    }    else{           return NO;    }}

 

Finally, set the Controller navigationbar attribute.

 [self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];   [ self.navigationController.navigationBar setTitleTextAttributes:@{                                                            NSForegroundColorAttributeName :[UIColor whiteColor]                                                                         }];

Related Article

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.