[Code Note] Unlock by gesture and unlock by note gesture

Source: Internet
Author: User

[Code Note] Unlock by gesture and unlock by note gesture

Animation unlocking-file directory

 
ViewController. h file
// Create a custom View, observe the Protocol, set the proxy, and implement the proxy method
1 # import "LYPaintView. h "2 3 # import" ViewController. h "4 5 @ interface ViewController () <LYPaintViewDelegate> 6 7 @ end 8 9 @ implementation ViewController10 11-(void) viewDidLoad {12 [super viewDidLoad]; 13 self. view. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "bg"]; 14 // load the View15 LYPaintView * paintView = [LYPaintView paintView]; 16 // set proxy 17 paintView. delegate = self; 18 19 paintView. center = self. view. center; 20 // Add View21 [self. view addSubview: paintView]; 22} 23 // modify the status bar status 24-(UIStatusBarStyle) preferredStatusBarStyle {25 return UIStatusBarStyleLightContent; 26} 27 // implement proxy method 28-(BOOL) checkPwd :( LYPaintView *) paintView withPwd :( NSString *) pawd {29 if ([pawd isEqualToString: @ "345"]) {30 return YES; 31} 32 return NO; 33} 34 @ end
LYPaintView. h
// File content, mainly for creating protocols and setting proxy Properties
1 # import <UIKit/UIKit. h> 2 @ class LYPaintView; 3 // create protocol 4 @ protocol LYPaintViewDelegate <NSObject> 5 // protocol Method 6-(BOOL) checkPwd :( LYPaintView *) paintView withPwd :( NSString *) pawd; 7 8 @ end 9 10 @ interface LYPaintView: UIView11 + (instancetype) paintView; 12 @ property (nonatomic, weak) id <LYPaintViewDelegate> delegate; 13 @ end
LYPaintView.m

// Unlock

# Import "LYPaintView. h "# define CZLineColor [UIColor colorWithRed: 0.0 green: 170/255. 0 blue: 255/255. 0 alpha: 0.5] @ interface LYPaintView () @ property (nonatomic, strong) NSMutableArray * btnsArray; @ property (nonatomic, strong) UIColor * lineColor; @ property (nonatomic, assign) CGPoint currentPoint; @ end @ implementation LYPaintView // Method for quickly creating an object class + (instancetype) paintView {return [[[NSBundle mainBundle] loadNibNamed: @ "LYPaintView" owner: nil options: nil] lastObject];} // start touch event-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// obtain the touch point UITouch * touch = touches. anyObject; CGPoint point = [touch locationInView: self]; // you can view whether the touch point is on the button [self. subviews enumerateObjectsUsingBlock: ^ (_ kindof UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {BOOL isContain = CGRectContainsPoint (obj. frame, point); if (isContain & obj. highlighted = NO) {obj. highlighted = YES; [self. btnsArray addObject: obj];} else {obj. highlighted = NO ;}}] ;}// when you move a cell by touch-(void) touchesMoved :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// obtain the current touch point UITouch * touch = touches. anyObject; CGPoint point = [touch locationInView: self]; // set the current vertex self. currentPoint = point; // click the traverse button to check whether the touch point is on the button [self. subviews enumerateObjectsUsingBlock: ^ (_ kindof UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {BOOL isContain = CGRectContainsPoint (obj. frame, point); if (isContain & obj. highlighted = NO) {obj. highlighted = YES; [self. btnsArray addObject: obj] ;}}]; // re-paint [self setNeedsDisplay] ;}// when the touch ends-(void) touchesEnded :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// eliminate the last line UIButton * lastBtn = self. btnsArray. lastObject; self. currentPoint = lastBtn. center; [self setNeedsDisplay]; // traverses the touch button to obtain the entered password NSMutableString * password = [NSMutableString]; [self. btnsArray enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {NSInteger tag = obj. tag; [password appendFormat: @ "% @", @ (tag)] ;}]; BOOL isTrue; // The proxy determines whether the password is consistent if ([self. delegate respondsToSelector: @ selector (checkPwd: withPwd :)]) {isTrue = [self. delegate checkPwd: self withPwd: password];} if (isTrue) {NSLog (@ "correct password"); // restore the status of the button when the password is correct [self. btnsArray enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {obj. highlighted = NO;}]; [self. btnsArray removeAllObjects]; [self setNeedsDisplay];} else {NSLog (@ "Incorrect password"); // The line turns red when the password is incorrect. self. userInteractionEnabled = NO; [self. btnsArray enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {obj. highlighted = NO; obj. enabled = NO;}]; self. lineColor = [UIColor redColor]; [self setNeedsDisplay]; // the original state of the restore button with a delay of 1 second dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (1.0 * NSEC_PER_SEC )), dispatch_get_main_queue (), ^ {[self. btnsArray enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {obj. enabled = YES;}]; [self. btnsArray removeAllObjects]; [self setNeedsDisplay]; self. userInteractionEnabled = YES; self. lineColor = CZLineColor;}) ;}// Load button-(void) awakeFromNib {self. lineColor = CZLineColor; for (int I = 0; I <9; I ++) {// create a button and add UIButton * btn = [[UIButton alloc] init]; btn. tag = I; btn. userInteractionEnabled = NO; [btn failed: [UIImage imageNamed: @ "gesture_node_normal"] forState: UIControlStateNormal]; [btn failed: [UIImage imageNamed: @ "success"] forState: Success]; [btn setBackgroundImage: [UIImage imageNamed: @ "gesture_node_error"] forState: UIControlStateDisabled]; [self addSubview: btn] ;}}// frame-(void) of the Set button) layoutSubviews {[super layoutSubviews]; int conNum = 3; CGFloat btnW = 70; CGFloat btnH = 70; CGFloat magin = (self. frame. size. width-conNum * btnW)/(conNum-1); [self. subviews enumerateObjectsUsingBlock: ^ (_ kindof UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {NSInteger col = idx % conNum; NSInteger row = idx/conNum; CGFloat btnX = col * (magin + btnW); CGFloat btnY = row * (magin + btnH); obj. frame = CGRectMake (btnX, btnY, btnW, btnH);}];} // draw a line using the drawRect method-(void) drawRect :( CGRect) rect {// do not plot if (self. btnsArray. count = 0) {return;} // Creation path UIBezierPath * path = [UIBezierPath bezierPath]; [self. btnsArray enumerateObjectsUsingBlock: ^ (UIButton * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {if (idx = 0) {[path moveToPoint: obj. center];} else {[path addLineToPoint: obj. center] ;}}]; // Add the final line segment [path addLineToPoint: self. currentPoint]; [self. lineColor setStroke]; [path setLineWidth: 10]; [path setLineCapStyle: kCGLineCapRound]; [path setLineJoinStyle: kCGLineJoinRound]; // render [path stroke];} // lazy load button array-(NSMutableArray *) btnsArray {if (_ btnsArray = nil) {_ btnsArray = [NSMutableArray array];} return _ btnsArray;} @ end

 

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.