IOS learning notes gesture password (original), ios2015-04-15
//// WPSignPasswordView. h // network cast /// Created by wangtouwang on 15/4/9. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <UIKit/UIKit. h> @ class WPSignPasswordView; @ protocol WPSignPasswordDelegate <NSObject> // The password is successfully set. @ required-(void) setPawSuccess :( NSString *) password; // The first temporary password is successfully set @ required-(void) setFirstPasswordSuccess :( NSString *) password; // The second input confirms that the password is incorrect-(void) setTwoPasswordError; // to change the gesture password, enter the previous password-(void) setSuccessAfterFirstPS :( NSString *) password; // enter the program and enter the gesture password to determine whether the password is correct-(void) confirmPassword :( NSString *) password; // The gesture password enters the modification state (that is, the original password is successfully entered). The first input is-(void) updateSPFirst :( NSString *) password; // The gesture password enters the modification state (that is, the original password is successfully entered). The second input is equivalent to entering the password for confirmation-(void) updateSPConfirm :( NSString *) password; @ end # pragma mark gesture password View @ interface WPSignPasswordView: UIView // set proxy @ property (nonatomic, strong) id <WPSignPasswordDelegate> spDelegate; @ end
//// WPSignPasswordView. m // network casting // Created by wangtouwang on 15/4/9. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import "WPSignPasswordView. h "// The Screen length and width # define KSCREEN_WIDTH [UIScreen mainScreen]. bounds. size. width # define KSCREEN_HEIGHT [UIScreen mainScreen]. bounds. size. height @ interface WPSignPasswordView () {}@ property (nonatomic, strong) NSMutableArray * stringArrays; @ property (nonatomic, st Rong) NSMutableArray * allButtonsArray; // defines an attribute and records the current point @ property (nonatomic, assign) CGPoint currentPoint; @ end @ implementation WPSignPasswordView # pragma mark instantiates an array of collection strings and loads it with laziness-(NSMutableArray *) getStringArrays {if (self. stringArrays = nil) {self. stringArrays = [NSMutableArray array];} return self. stringArrays ;}# pragma mark instantiates an array containing all password buttons and uses the lazy load-(NSMutableArray *) getAllButtonsArray {if (self. allButtonsA Rray = nil) {self. allButtonsArray = [NSMutableArray array];} return self. allButtonsArray ;}# pragma mark rewrite initialization interface function initFrame-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {[self setup] ;}return self ;}# pragma mark interface Layout-(void) setup {// NSLog (@ "initialized interface layout executed "); for (int index = 1; index <= 9; index ++) {// create the button UIButton * numberButton = [[UIButton alloc] init]; // set Specifies the background image of the button and the status of [numberButton setBackgroundImage: [UIImage imageNamed: @ "gesture_node_normal"] forState: UIControlStateNormal]; [numberButton failed: [UIImage imageNamed: @ "gesture_node_highlighted"] forState: UIControlStateSelected]; // Add the button to the view [self addSubview: numberButton]; // store the button in the button array [[self getAllButtonsArray] addObject: numberButton]; // disable the button to click the event numberButton. userInteractionEnabled = NO; // set the button flag value numberButton. tag = index ;}# pragma mark to see when to trigger-(void) layoutSubviews {// The method of the parent class needs to be called first [super layoutSubviews]; // set the button position for (int index = 0; index <self. allButtonsArray. count; index ++) {CGFloat inverst_top = KSCREEN_HEIGHT/4; inverst_top = 0; UIButton * btn = (UIButton *) self. allButtonsArray [index]; // NSLog (@ "% I", btn. tag); // obtain the row number CGFloat row = index/3; // obtain the coordinate number CGFloat loc = index % 3; // The button length and width CG Float btnHeight = 75; CGFloat btnWith = btnHeight; // get the button interval from CGFloat inverst = (KSCREEN_WIDTH-btnHeight * 3)/4; // x y coordinate CGFloat btnX = inverst + loc * (btnWith + inverst); CGFloat btnY = inverst + row * (btnHeight + inverst); btn. frame = CGRectMake (btnX, btnY + inverst_top-15, btnHeight, btnHeight) ;}# pragma mark rewrite canvas Layout-(void) drawRect :( CGRect) rect {// NSLog (@ "trigger again"); // obtain the context CGContextRef context = UIGraphicsG EtCurrentContext (); # pragma mark fill canvas color // fill context color CGContextSetFillColorWithColor (context, [UIColor grayColor] CGColor]); // rect CGContextFillRect (context, context, rect); # pragma mark-achieve the draw line function int I = 0; // drawing (Line Segment) for (UIButton * btn in self. stringArrays) {if (0 = I) {// set the start point (note that the point is connected) CGContextMoveToPoint (context, btn. center. x, btn. center. y);} else {CGContextAddLineToPoint (context, btn. center. x, btn. c Enter. y);} I ++;} // when the midpoint of all buttons is connected, connect the current position of the finger. // check whether there are buttons in the array, if (self. stringArrays. count! = 0) {// draw a straight CGContextAddLineToPoint (context, self. currentPoint. x, self. currentPoint. y);} // render // set the line width CGContextSetLineWidth (context, 10); // set the connection style in the image context. CGContextSetLineJoin (context, kCGLineJoinRound); // you can specify CGContextSetLineCap (context, kCGLineCapRound) as the end point of a line. // set CGContextSetRGBStrokeColor (context, 255/255) as the paint color. 0, 100/255. 0, 0/255. 0, 1); // start to draw the image CGContextStrokePath (context) ;}# pragma mark rewrite UIResponder -- API listener finger movement start-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {UITouch * uitouch = [touches anyObject]; CGPoint point = [uitouch loc AtionInView: uitouch. view]; UIButton * targetBtn = nil; for (UIButton * btn in self. allButtonsArray) {if (CGRectContainsPoint (btn. frame, point) {targetBtn = btn; break ;}} if (targetBtn & targetBtn. selected! = YES) {targetBtn. selected = YES; [[self getStringArrays] addObject: targetBtn] ;}# pragma mark replay UIResponder -- API listener finger movement-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {UITouch * uitouch = [touches anyObject]; CGPoint movepoint = [uitouch locationInView: uitouch. view]; UIButton * targetBtn = nil; for (UIButton * btn in self. allButtonsArray) {if (CGRectContainsPoint (btn. frame, movepoint) {TargetBtn = btn; break;} if (targetBtn & targetBtn. selected! = YES) {// select the targetBtn status of the set button. selected = YES; // Add the button to the array [[self getStringArrays] addObject: targetBtn];} // record the current vertex (not within the scope of the button) self. currentPoint = movepoint; // notify the view to re-draw [self setNeedsDisplay];} # pragma mark re-write UIResponder -- API listener finger to exit the screen-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {// retrieve the password entered by the user and create a variable string, which is used to save the user password and retrieve the password NSMutableString * result = [NSMutableString string]; for (UIButton * Btn in self. stringArrays) {[result appendFormat: @ "% lu", btn. tag];} // NSLog (@ "the password entered by the user is: % @", result); '// clear the connection record [self. stringArrays makeObjectsPerformSelector: @ selector (setSelected :) withObject: @ (NO)]; // clear the array [self. stringArrays removeAllObjects]; [self setNeedsDisplay]; // clear the current vertex self. currentPoint = CGPointZero; // obtain the nsuserults ults object and check whether the gesture password NSUserDefaults * userDefaults = [NSUserDefaults standard exists in the object UserDefaults]; BOOL flag = [userDefaults boolForKey: @ "flag"]; NSInteger temp_signpaw = [userDefaults integerForKey: @ "temp_signpaw"]; if (! Flag) {# pragma mark-Password not set // record temporary password if (temp_signpaw = 0) {if ([self. spDelegate respondsToSelector: @ selector (setFirstPasswordSuccess :)]) {[self. spDelegate setFirstPasswordSuccess: result] ;}// NSLog (@ "% I", temp_signpaw); if (temp_signpaw! = 0 & temp_signpaw = [result integerValue]) {// jump to if ([self. spDelegate respondsToSelector: @ selector (setPawSuccess :)]) {[self. spDelegate setPawSuccess: result] ;}} else if (temp_signpaw! = 0 & temp_signpaw! = [Result integerValue]) {// error handling if ([self. spDelegate respondsToSelector: @ selector (setTwoPasswordError)]) {[self. spDelegate setTwoPasswordError] ;}} else {# pragma mark-the password has been set to NSInteger status = [userDefaults integerForKey: @ "status"]; if (status =-1) {// NSLog (@ "Debug status");} else {// normal status // determines the NSString * page = [userDefaults objectForKey: @ "turnPage"]; if ([page isEqualToString: @ "mainPage"]) {If ([self. spDelegate respondsToSelector: @ selector (confirmPassword :)]) {[self. spDelegate confirmPassword: result] ;}} else {NSInteger updateStatus = [userDefaults integerForKey: @ "updateStatus"]; if (updateStatus = 1) {NSInteger tempUpdate_signpaw = [userDefaults integerForKey: @ "tempUpdate_signpaw"]; if (tempUpdate_signpaw = 0) {if ([self. spDelegate respondsToSelector: @ selector (updateSPFirst :)]) {[Self. spDelegate updateSPFirst: result] ;}} if (tempUpdate_signpaw! = 0) {if ([self. spDelegate respondsToSelector: @ selector (updateSPConfirm :)]) {[self. spDelegate updateSPConfirm: result] ;}} if (updateStatus! = 1) {// determine whether the gesture password has been successfully entered if ([self. spDelegate respondsToSelector: @ selector (setSuccessAfterFirstPS :)]) {[self. spDelegate setSuccessAfterFirstPS: result] ;}}}}@ end