iOS實(shí)現(xiàn)手勢(shì)密碼功能
手勢(shì)密碼實(shí)現(xiàn)
手勢(shì)密碼 一般常常用于金融項(xiàng)目,做的是安全相關(guān)的業(yè)務(wù)。具體實(shí)現(xiàn)如下思路,我把它分為view層和邏輯層。我將數(shù)據(jù)層合并到view層中了,最好是加上數(shù)據(jù)層用于處理加密的密碼和密碼的存儲(chǔ)
view層
view層主要處理,包括(九個(gè)按鈕)touchesBegan,touchesMoved,touchesEnded,點(diǎn)與點(diǎn)之間畫(huà)線,手指滑動(dòng)畫(huà)線,畫(huà)線主要是在drawRect中重繪,提到這里必須不能忘記setNeedsDisplay這個(gè)方法。還要記錄經(jīng)過(guò)的按鈕btnsArray(存放按鈕的數(shù)組),這個(gè) 可以和相關(guān)的具體值做映射,也可以直接設(shè)置btn 的tag,還要添加完成繪畫(huà)的回調(diào)。提供給邏輯層去處理。
邏輯層
用于處理完成交互后的業(yè)務(wù),包括(請(qǐng)求接口,異常邏輯顯示,等等)
具體的demo點(diǎn)這里
具體的code:
view.h
// // YHGesturePasswordView.h // 手勢(shì)密碼 // // Created by mrlee on 2017/3/5. // Copyright © 2017年 mrlee. All rights reserved. // typedef enum { GestureSetPassword, //設(shè)置手勢(shì)密碼 GestureResultPassword //已有手勢(shì)密碼教驗(yàn) } PasswordState; //設(shè)置密碼的3種狀態(tài) typedef enum { FristPwd, //第一次設(shè)置密碼 PwdNoValue, //二次設(shè)置密碼不一致 SetPwdSuccess, //設(shè)置密碼成功 Other }SetPwdState; #import <UIKit/UIKit.h> @interface YHGesturePasswordView : UIView /** btn圖片*/ @property (nonatomic,strong)UIImage *btnImage; ///選中的圖片 @property (nonatomic,strong)UIImage *btnSelectImage; ///劃線顏色 @property (nonatomic,strong)UIColor *lineColor; /** 解鎖手勢(shì)完成之后判斷結(jié)果時(shí)調(diào)用的block */ @property (nonatomic,copy)BOOL (^sendReaultData)(NSString *str); //設(shè)置手勢(shì)密碼 @property(nonatomic,copy)void(^setPwdBlock)(SetPwdState pwdState); // init -(instancetype)initWithFrame:(CGRect)frame WithState:(PasswordState)state; @end
view.m
// // YHGesturePasswordView.m // 手勢(shì)密碼 // // Created by mrlee on 2017/3/5. // Copyright © 2017年 mrlee. All rights reserved. // #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #import "YHCustomButton.h" #import "YHGesturePasswordView.h" #import <CommonCrypto/CommonDigest.h> @interface YHGesturePasswordView(){ /** 判斷是當(dāng)設(shè)置密碼用,還是解鎖密碼用*/ PasswordState Amode; } /** 所有的按鈕集合*/ @property (nonatomic,strong)NSMutableArray * allBtnsArray; /** 解鎖時(shí)手指經(jīng)過(guò)的所有的btn集合*/ @property (nonatomic,strong)NSMutableArray * btnsArray; /** 手指當(dāng)前的觸摸位置*/ @property (nonatomic,assign)CGPoint currentPoint; @end @implementation YHGesturePasswordView -(instancetype)initWithFrame:(CGRect)frame WithState:(PasswordState)state{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; Amode = state; for (int i = 0; i<9; i++) { YHCustomButton *btn = [[YHCustomButton alloc]init]; [btn setTag:i]; btn.userInteractionEnabled = NO; if (self.lineColor == nil) { self.lineColor = [UIColor greenColor]; } [self addSubview:btn]; } } return self; } -(void)drawRect:(CGRect)rect{ // 每次調(diào)用這個(gè)方法的時(shí)候如果背景顏色是default會(huì)產(chǎn)生緩存,如果設(shè)置了顏色之后就沒(méi)有緩存,繪制之前需要清除緩存 CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextClearRect(ctx, rect);//清空上下文 for (int i = 0; i<self.btnsArray.count; i++) { UIButton *btn = self.btnsArray[i]; if (i == 0) { CGContextMoveToPoint(ctx, btn.center.x, btn.center.y); }else{ CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y); } } if (!CGPointEqualToPoint(self.currentPoint, CGPointZero)) {//如果起點(diǎn)不是CGPointZero的話才來(lái)劃線 CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y); } CGContextSetLineWidth(ctx, 12); CGContextSetLineCap(ctx, kCGLineCapRound); CGContextSetLineJoin(ctx, kCGLineJoinRound); [self.lineColor set]; CGContextStrokePath(ctx); } -(void)layoutSubviews{ [self.allBtnsArray removeAllObjects]; for (int index =0; index<self.subviews.count; index ++) { if ([self.subviews[index] isKindOfClass:[YHCustomButton class]]) { [self.allBtnsArray addObject:self.subviews[index]]; } } // button 繪制九宮格 [self drawUi]; } #pragma mark Private method -(void)drawUi{ for (int index = 0; index<self.allBtnsArray.count; index ++) { //拿到每個(gè)btn UIButton *btn = self.subviews[index]; //設(shè)置frame CGFloat btnW = 74; CGFloat btnH = 74; CGFloat margin = (SCREEN_WIDTH - (btnW *3))/4; //x = 間距 + 列號(hào)*(間距+btnW) CGFloat btnX = margin + (index % 3)*(margin + btnW); CGFloat btnY = margin + (index / 3)*(margin + btnH); btn.frame = CGRectMake(btnX, btnY, btnW, btnH); } } //設(shè)置密碼 -(SetPwdState)pwdValue:(NSString *)str{ if ([[NSUserDefaults standardUserDefaults] objectForKey:@"pwdValue"] == nil) { //第一次設(shè)置 [[NSUserDefaults standardUserDefaults] setValue:str forKey:@"pwdValue"]; return FristPwd; } if ([str isEqualToString: [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdValue"]]) { //設(shè)置成功 return SetPwdSuccess; } if (![str isEqualToString: [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdValue"]]) { //二次設(shè)置不一樣 return PwdNoValue; } return Other; } //清空 -(void)clear{ [self.btnsArray removeAllObjects]; self.currentPoint = CGPointZero; [self setNeedsDisplay]; self.lineColor = [UIColor greenColor]; self.userInteractionEnabled = YES; } //獲取觸摸的點(diǎn) -(CGPoint)getCurrentTouch:(NSSet<UITouch*> *)touches{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:touch.view]; return point; } -(UIButton *)getCurrentBtnWithPoint:(CGPoint) currentPoint{ for (UIButton *btn in self.subviews) { if (CGRectContainsPoint(btn.frame, currentPoint)) { return btn; } } return nil; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ CGPoint point = [self getCurrentTouch:touches]; UIButton *btn = [self getCurrentBtnWithPoint:point]; if (btn && btn.selected != YES) { btn.selected = YES; [self.btnsArray addObject:btn]; NSLog(@" array is value %@",self.btnsArray); } } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ CGPoint movePoint = [self getCurrentTouch:touches]; UIButton *btn = [self getCurrentBtnWithPoint:movePoint]; if (btn && btn.selected !=YES) { btn.selected = YES; [self.btnsArray addObject:btn]; NSLog(@"btn is value %@",self.btnsArray); } self.currentPoint = movePoint; [self setNeedsDisplay]; } -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ for (UIButton *btn in self.btnsArray) { [btn setSelected:NO]; } NSMutableString *result = [NSMutableString string]; for (UIButton *btn in self.btnsArray) { [result appendString: [NSString stringWithFormat:@"%ld",(long)btn.tag]]; } switch (Amode) { case GestureSetPassword:{ //設(shè)置手勢(shì)密碼 self.setPwdBlock([self pwdValue:result]); } break; case GestureResultPassword :{ //獲取手勢(shì)密碼結(jié)果 if (self.sendReaultData) { if (self.sendReaultData(result) == YES) { NSLog(@"success"); [self clear]; }else{ NSLog(@"手勢(shì)有誤"); } } } break; default: break; } //返回結(jié)果 [self clear]; } #pragma mark 延時(shí)加載 -(NSMutableArray *)btnsArray{ if (_btnsArray == nil) { _btnsArray = [NSMutableArray array]; } return _btnsArray; } -(NSMutableArray *)allBtnsArray{ if (_allBtnsArray == nil) { _allBtnsArray = [NSMutableArray array]; } return _allBtnsArray; } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS應(yīng)用中UICollectionViewCell定制Button
這篇文章主要介紹了iOS應(yīng)用中UICollectionViewCell如何定制Button,設(shè)置每行顯示的按鈕的個(gè)數(shù),自定制按鈕的顯示樣式,感興趣的小伙伴們可以參考一下2016-08-08iOS中setValue和setObject的區(qū)別詳解
setObject:ForKey: 是NSMutableDictionary特有的;setValue:ForKey:是KVC的主要方法。接下來(lái)通過(guò)本文給大家分享iOS中setValue和setObject的區(qū)別,需要的朋友參考下2017-02-02iOS?項(xiàng)目嵌入Flutter?運(yùn)行(最新推薦)
這篇文章主要介紹了iOS?項(xiàng)目嵌入Flutter?運(yùn)行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03IOS 時(shí)間和時(shí)間戳之間轉(zhuǎn)化示例
我們經(jīng)常從服務(wù)器后臺(tái)拿到時(shí)間戳的時(shí)間,以下代碼可以實(shí)現(xiàn)將時(shí)間戳轉(zhuǎn)為可讀的時(shí)間格式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01分享一個(gè)iOS下實(shí)現(xiàn)基本繪畫(huà)板功能的簡(jiǎn)單方法
這篇文章主要介紹了iOS下實(shí)現(xiàn)基本繪畫(huà)板功能的簡(jiǎn)單方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10IOS開(kāi)發(fā)仿微信消息長(zhǎng)按氣泡菜單實(shí)現(xiàn)效果
這篇文章主要介紹了IOS開(kāi)發(fā)仿微信消息長(zhǎng)按氣泡菜單實(shí)現(xiàn)效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07IOS開(kāi)發(fā)之路--C語(yǔ)言存儲(chǔ)方式和作用域
只有你完全了解每個(gè)變量或函數(shù)存儲(chǔ)方式、作用范圍和銷(xiāo)毀時(shí)間才可能正確的使用這門(mén)語(yǔ)言。今天將著重介紹C語(yǔ)言中變量作用范圍、存儲(chǔ)方式、生命周期、作用域和可訪問(wèn)性。2014-08-08iOS UIBezierPath實(shí)現(xiàn)餅狀圖
這篇文章主要為大家詳細(xì)介紹了iOS UIBezierPath實(shí)現(xiàn)餅狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03