iOS实现手势密码功能
手势密码实现
手势密码一般常常用于金融项目,做的是安全相关的业务。具体实现如下思路,我把它分为view层和逻辑层。我将数据层合并到view层中了,最好是加上数据层用于处理加密的密码和密码的存储
view层
view层主要处理,包括(九个按钮)touchesBegan,touchesMoved,touchesEnded,点与点之间画线,手指滑动画线,画线主要是在drawRect中重绘,提到这里必须不能忘记setNeedsDisplay这个方法。还要记录经过的按钮btnsArray(存放按钮的数组),这个可以和相关的具体值做映射,也可以直接设置btn的tag,还要添加完成绘画的回调。提供给逻辑层去处理。
逻辑层
用于处理完成交互后的业务,包括(请求接口,异常逻辑显示,等等)
具体的demo点这里
具体的code:
view.h
//
//YHGesturePasswordView.h
//手势密码
//
//Createdbymrleeon2017/3/5.
//Copyright©2017年mrlee.Allrightsreserved.
//
typedefenum{
GestureSetPassword,//设置手势密码
GestureResultPassword//已有手势密码教验
}PasswordState;
//设置密码的3种状态
typedefenum{
FristPwd,//第一次设置密码
PwdNoValue,//二次设置密码不一致
SetPwdSuccess,//设置密码成功
Other
}SetPwdState;
#import
@interfaceYHGesturePasswordView:UIView
/**btn图片*/
@property(nonatomic,strong)UIImage*btnImage;
///选中的图片
@property(nonatomic,strong)UIImage*btnSelectImage;
///划线颜色
@property(nonatomic,strong)UIColor*lineColor;
/**解锁手势完成之后判断结果时调用的block*/
@property(nonatomic,copy)BOOL(^sendReaultData)(NSString*str);
//设置手势密码
@property(nonatomic,copy)void(^setPwdBlock)(SetPwdStatepwdState);
//init
-(instancetype)initWithFrame:(CGRect)frameWithState:(PasswordState)state;
@end
view.m
// //YHGesturePasswordView.m //手势密码 // //Createdbymrleeon2017/3/5. //Copyright©2017年mrlee.Allrightsreserved. // #defineSCREEN_WIDTH[UIScreenmainScreen].bounds.size.width #defineSCREEN_HEIGHT[UIScreenmainScreen].bounds.size.height #import"YHCustomButton.h" #import"YHGesturePasswordView.h" #import@interfaceYHGesturePasswordView(){ /**判断是当设置密码用,还是解锁密码用*/ PasswordStateAmode; } /**所有的按钮集合*/ @property(nonatomic,strong)NSMutableArray*allBtnsArray; /**解锁时手指经过的所有的btn集合*/ @property(nonatomic,strong)NSMutableArray*btnsArray; /**手指当前的触摸位置*/ @property(nonatomic,assign)CGPointcurrentPoint; @end @implementationYHGesturePasswordView -(instancetype)initWithFrame:(CGRect)frameWithState:(PasswordState)state{ self=[superinitWithFrame:frame]; if(self){ self.backgroundColor=[UIColorclearColor]; Amode=state; for(inti=0;i<9;i++){ YHCustomButton*btn=[[YHCustomButtonalloc]init]; [btnsetTag:i]; btn.userInteractionEnabled=NO; if(self.lineColor==nil){ self.lineColor=[UIColorgreenColor]; } [selfaddSubview:btn]; } } returnself; } -(void)drawRect:(CGRect)rect{ //每次调用这个方法的时候如果背景颜色是default会产生缓存,如果设置了颜色之后就没有缓存,绘制之前需要清除缓存 CGContextRefctx=UIGraphicsGetCurrentContext(); CGContextClearRect(ctx,rect);//清空上下文 for(inti=0;i *)touches{ UITouch*touch=[touchesanyObject]; CGPointpoint=[touchlocationInView:touch.view]; returnpoint; } -(UIButton*)getCurrentBtnWithPoint:(CGPoint)currentPoint{ for(UIButton*btninself.subviews){ if(CGRectContainsPoint(btn.frame,currentPoint)){ returnbtn; } } returnnil; } -(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{ CGPointpoint=[selfgetCurrentTouch:touches]; UIButton*btn=[selfgetCurrentBtnWithPoint:point]; if(btn&&btn.selected!=YES){ btn.selected=YES; [self.btnsArrayaddObject:btn]; NSLog(@"arrayisvalue%@",self.btnsArray); } } -(void)touchesMoved:(NSSet *)toucheswithEvent:(UIEvent*)event{ CGPointmovePoint=[selfgetCurrentTouch:touches]; UIButton*btn=[selfgetCurrentBtnWithPoint:movePoint]; if(btn&&btn.selected!=YES){ btn.selected=YES; [self.btnsArrayaddObject:btn]; NSLog(@"btnisvalue%@",self.btnsArray); } self.currentPoint=movePoint; [selfsetNeedsDisplay]; } -(void)touchesEnded:(NSSet *)toucheswithEvent:(UIEvent*)event{ for(UIButton*btninself.btnsArray){ [btnsetSelected:NO]; } NSMutableString*result=[NSMutableStringstring]; for(UIButton*btninself.btnsArray){ [resultappendString:[NSStringstringWithFormat:@"%ld",(long)btn.tag]]; } switch(Amode){ caseGestureSetPassword:{ //设置手势密码 self.setPwdBlock([selfpwdValue:result]); } break; caseGestureResultPassword:{ //获取手势密码结果 if(self.sendReaultData){ if(self.sendReaultData(result)==YES){ NSLog(@"success"); [selfclear]; }else{ NSLog(@"手势有误"); } } } break; default: break; } //返回结果 [selfclear]; } #pragmamark延时加载 -(NSMutableArray*)btnsArray{ if(_btnsArray==nil){ _btnsArray=[NSMutableArrayarray]; } return_btnsArray; } -(NSMutableArray*)allBtnsArray{ if(_allBtnsArray==nil){ _allBtnsArray=[NSMutableArrayarray]; } return_allBtnsArray; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。