1. 程式人生 > >解鎖手勢功能,顯示痕跡和不顯示痕跡只要看註釋就可以

解鎖手勢功能,顯示痕跡和不顯示痕跡只要看註釋就可以

model 是否 pen uibutton int rst format image 結束

#import "LoginGestureCodeViewController.h"
#import "UserModel.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

#define BtnTag 5000

@interface LoginGestureCodeViewController ()

@property (nonatomic,strong)NSMutableArray *buttonArr;//全部手勢按鍵的數組
@property (nonatomic,strong)NSMutableArray *selectorArr;//
選中手勢按鍵的數組 @property (nonatomic,assign)CGPoint startPoint;//記錄開始選中的按鍵坐標 @property (nonatomic,assign)CGPoint endPoint;//記錄結束時的手勢坐標 @property (nonatomic,strong)UIImageView *imageView;//畫圖所需 @property (nonatomic,strong)NSMutableString *handMoveString;//最終手勢的字符串 @end @implementation LoginGestureCodeViewController
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; self.navigationItem.title = @"驗證手勢"; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = getColor(bgColor); if (!_buttonArr) { _buttonArr = [[NSMutableArray alloc] initWithCapacity:9
]; } self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)]; [self.view addSubview:self.imageView]; //提示label UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT/5 - 40 / WIDTH_5S_SCALE, SCREEN_WIDTH, 14/ WIDTH_5S_SCALE)]; textLabel.font = DEF_FontSize_14; textLabel.textColor = getColor(mainColor); textLabel.textAlignment = NSTextAlignmentCenter; textLabel.text = @"請輸入手勢密碼"; [self.view addSubview:textLabel]; //創建按鈕 for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; //設置TAG值 if (i == 0) { btn.tag = BtnTag + j; }else if (i == 1){ btn.tag = BtnTag + 3 + j; }else{ btn.tag = BtnTag + 6 + j; } btn.frame = CGRectMake(SCREEN_WIDTH/6+SCREEN_WIDTH/4*j, SCREEN_HEIGHT/5+SCREEN_WIDTH/4*i, SCREEN_WIDTH/5, SCREEN_WIDTH/5); [btn setImage:[UIImage imageNamed:@"login_shoushi1"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"login_shoushi2"] forState:UIControlStateHighlighted]; btn.imageView.contentMode = UIViewContentModeScaleAspectFit; btn.userInteractionEnabled = NO; [self.buttonArr addObject:btn]; [self.imageView addSubview:btn]; } } //確認按鈕 UIButton *commitButton = [[UIButton alloc] initWithFrame:CGRectMake(30 / WIDTH_5S_SCALE, SCREEN_HEIGHT - 64 - 40 - 50 / WIDTH_5S_SCALE, SCREEN_WIDTH - 60 / WIDTH_5S_SCALE, 40)]; commitButton.titleLabel.font = DEF_FontSize_18; [commitButton setTitle:@"重置手勢" forState:UIControlStateNormal]; [commitButton setTitleColor:getColor(whiteColor) forState:UIControlStateNormal]; [commitButton setBackgroundColor:getColor(mainColor)]; commitButton.layer.cornerRadius = 5; commitButton.layer.masksToBounds = YES; [commitButton addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:commitButton]; } //畫線 - (UIImage *)drawLine{ UIImage *image = nil; UIColor *col = getColor(mainColor); //設置畫圖的大小為imageview的大小 UIGraphicsBeginImageContext(self.imageView.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2); CGContextSetStrokeColorWithColor(context, col.CGColor); //設置畫線起點 CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y); //從起點畫線到選中的按鍵中心,並切換畫線的起點 for (UIButton *btn in self.selectorArr) { CGPoint btnPo = btn.center; CGContextAddLineToPoint(context, btnPo.x, btnPo.y); CGContextMoveToPoint(context, btnPo.x, btnPo.y); } //畫移動中的最後一條線 CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y); CGContextStrokePath(context); image = UIGraphicsGetImageFromCurrentImageContext();//畫圖輸出 UIGraphicsEndImageContext();//結束畫線 return image; } //開始手勢 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ self.imageView.image = nil; self.selectorArr = nil; for (UIButton *btn in self.buttonArr) { btn.highlighted = NO; } UITouch *touch = [touches anyObject]; if (touch) { //記錄起點 self.startPoint = [touch locationInView:self.imageView]; for (UIButton *btn in self.buttonArr) { //記錄按鍵坐標 CGPoint po = [touch locationInView:btn]; //判斷按鍵坐標是否在手勢開始範圍內,是則為選中的開始按鍵 if ([btn pointInside:po withEvent:nil]) { [self.selectorArr addObject:btn]; btn.highlighted = YES; //保存起始坐標 self.startPoint = btn.center; } } } } //移動中 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if (touch) { self.endPoint = [touch locationInView:self.imageView]; for (UIButton *btn in self.buttonArr) { CGPoint po = [touch locationInView:btn]; if ([btn pointInside:po withEvent:nil]) { BOOL isAdd = YES;//記錄是否為重復按鍵 for (UIButton *seBtn in self.selectorArr) { if (seBtn == btn) { isAdd = NO;//已經是選中過的按鍵,不再重復添加 break; } } if (isAdd) {//未添加的選中按鍵,添加並修改狀態 [self.selectorArr addObject:btn]; btn.highlighted = YES; } } } } //每次移動過程中都要調用這個方法,把畫出的圖輸出顯示 self.imageView.image = [self drawLine]; } //手勢結束 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //手勢圖畫消失,打印已選擇的數組 self.imageView.image = nil; for (UIButton *btn in self.buttonArr) { btn.highlighted = NO; } self.startPoint = CGPointZero; NSMutableString *str = @"".mutableCopy; for (UIButton *btn in self.selectorArr) { NSLog(@"aaa -- %ld",btn.tag - BtnTag); str = (NSMutableString*)[str stringByAppendingString:[NSString stringWithFormat:@"%ld",btn.tag - BtnTag]]; } self.handMoveString = str; // 註:可變代碼 NSString *locanHandMove = [UserModel sharedInstanced].handMove; if ([locanHandMove isEqualToString:str]) { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; [appDelegate loginStateChange]; }else{ ShowMessage(@"手勢密碼錯誤"); } // //手勢圖畫不消失,打印已選擇的數組 // self.startPoint = CGPointZero; // for (UIButton *btn in self.selectorArr) { // NSLog(@"aaa -- %ld",btn.tag - BtnTag); // } } #pragma mark -- btnAction - (void)commitBtnAction{ NSLog(@"確認按鈕"); // NSLog(@"--- %@",self.selectorArr); // for (UIButton *btn in self.selectorArr) { // NSLog(@"aaa -- %ld",btn.tag - BtnTag); // } // 切換根控制器(可變代碼) UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]]; [UIApplication sharedApplication].delegate.window.rootViewController = nav; } #pragma mark -- init - (NSMutableArray *)selectorArr{ if (!_selectorArr) { _selectorArr = [[NSMutableArray alloc] init]; } return _selectorArr; } @end

解鎖手勢功能,顯示痕跡和不顯示痕跡只要看註釋就可以