本文重要引見經由過程手勢辨認完成手勢解鎖功效,這個辦法被普遍用於手機解鎖,暗碼驗證,快捷付出等功效完成。事例後果以下所示。

起首,我們先剖析功效的完成進程,起首我們須要先看年夜致的完成進程:
1.加載九宮格頁面
2.完成按鈕被點擊及滑動進程中按鈕狀況的轉變
3.完成滑動進程中的連線
4.繪制終了後剖斷暗碼能否准確,
5.暗碼剖斷後完成跳轉。
上面我們就來用代碼完成上述五個進程。
1.加載九宮格界面
1.1九宮格內控件的散布 3*3 ,我們可以自界說view(包括3*3個按鈕),添加到viewController上。
//添加view中子控件
-(void)awakeFromNib
{
// 創立按鈕
for (int i=0; i<9; i++) {
self.LineColor=[UIColor blueColor];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.userInteractionEnabled=NO;
// 設置按鈕屬性
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateHighlighted ];
[btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_error"] forState:UIControlStateDisabled];
[self addSubview:btn];
}
}
//結構view子控件
-(void)layoutSubviews
{
[super layoutSubviews];
CGFloat width=74;
CGFloat height=74;
CGFloat Margin=(self.bounds.size.width-3*width)/2;
// 遍歷設置9個button的frame
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 經由過程tag設置按鈕的索引標識
obj.tag=idx;
int row=(int)idx/3;
int col=idx%3;
obj.frame=CGRectMake(col*(Margin + width), row*(Margin +height), width, height);
}];
}
1.2將界說好的view經由過程xib添加到viewController上
起首,界說一個blockview(九宮格view)的類辦法,
// 加載xib文件
+(instancetype)lockView
{
return [[[NSBundle mainBundle]loadNibNamed:@"MYblockView" owner:nil options:nil]lastObject];
}
然後加載到掌握器上。
// 設置掌握器view的配景圖片 self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]]; MYblockView *blockView=[MYblockView lockView]; blockView.center=self.view.center; // 將blockview添加到viewController上 [self.view addSubview:blockView];
2.完成按鈕被點擊及滑動進程中按鈕狀況的轉變
2.1界說數組類型的成員屬性,用來裝被點擊的按鈕
@property(nonatomic,strong)NSMutableArray *btnArr;
//懶加載
-(NSMutableArray *)btnArr
{
if (_btnArr==nil) {
_btnArr=[NSMutableArray array];
}
return _btnArr;
}
2.2創立途徑,繪制圖形
#pragma mark----繪制圖形
-(void)drawRect:(CGRect)rect
{
if (self.btnArr.count==0 ) {
return;
}
// 創立途徑
UIBezierPath *path=[UIBezierPath bezierPath];
// 遍歷一切按鈕停止繪制
[self.btnArr enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 第一個按鈕,中間點就是終點
if (idx==0) {
[path moveToPoint:obj.center];
}else
{
[path addL.netoPoint:obj.center];
}
}];
[path addL.netoPoint:self.currentPoint];
// 設置途徑屬性
path.lineWidth=10;
path.lineCapStyle=kCGLineCapRound;
path.lineJoinStyle=kCGLineJoinRound;
[self.LineColor setStroke];
// 襯著
[path stroke];
}
2.3開端觸摸
#pragma mark-----開端觸摸
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 獲得觸摸對象
UITouch *touch=touches.anyObject;
// 獲得觸摸點
CGPoint loc=[touch locationInView:self];
// 遍歷按鈕,剖斷觸摸點能否在按鈕上
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
BOOL isContains=CGRectContainsPoint(obj.frame, loc);
// 假如在按鈕上,將以後按鈕保留在數組中,並轉變按鈕狀況
if (isContains&&obj.highlighted==NO) {
[self.btnArr addObject:obj];
obj.highlighted=YES;
}else
{
obj.highlighted=NO;
}
}];
}
2.4滑動進程中,重繪
#pragma mark----開端滑動
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 獲得觸摸對象
UITouch *touch=touches.anyObject;
// 獲得觸摸點
CGPoint loc=[touch locationInView:self];
self.currentPoint=loc;
// 遍歷按鈕,假如按鈕在滑動途徑上,就轉變按鈕狀況
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
BOOL isContains=CGRectContainsPoint(obj.frame, loc);
if (isContains&&obj.highlighted==NO) {
[self.btnArr addObject:obj];
obj.highlighted=YES;
}
}];
// 重繪
[self setNeedsDisplay];
}
3.完成滑動進程中的連線和4.繪制終了後剖斷暗碼能否准確
#pragma mark----停滯滑動停止
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 界說最初一個按鈕
UIButton *lastBtn=[self.btnArr lastObject];
// 將最初一個按鈕中間點界說為絕對滑動確當前點
self.currentPoint=lastBtn.center;
// 重繪
[self setNeedsDisplay];
// 剖斷暗碼
self.password=[NSMutableString string];
[self.btnArr enumerateObjectsUsingBlock:^( UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.password appendFormat:@"%@",@(obj.tag)];
}];
NSLog(@"%@",self.password);
BOOL isOk;
if ([self.delegate respondsToSelector:@selector(blockView:finishedWithPassword:)]) {
isOk= [self.delegate blockView:self finishedWithPassword:self.password];
}
if (isOk) {
[self.btnArr enumerateObjectsUsingBlock:^(UIButton* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.highlighted=NO;
}];
[self.btnArr removeAllObjects];
[self setNeedsDisplay];
NSLog(@"暗碼准確");
}else
{
NSLog(@"暗碼毛病");
}
}
留意:我們在暗碼剖斷進程中是經由過程依據先前結構按鈕的時刻界說的按鈕tag值停止字符串拼接,暗碼傳值是經由過程署理完成。
#import <UIKit/UIKit.h> @class MYblockView; //聲明朝理 @protocol MYblockViewDelegate <NSObject> @optional //署理辦法 -(BOOL) blockView:(MYblockView *)blockView finishedWithPassword:(NSString *)password; @end @interface MYblockView : UIView +(instancetype)lockView; //設置署理成員屬性 @property(nonatomic,weak)id<MYblockViewDelegate>delegate; @end
5.暗碼剖斷後完成跳轉。
else
{
// 封閉用戶交互
self.userInteractionEnabled=NO;
[self.btnArr enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
self.LineColor=[UIColor redColor];
obj.highlighted=NO;
obj.enabled=NO;
[self setNeedsDisplay];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 答復按鈕狀況
[self.btnArr enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.enabled=YES;
}];
// 恢單線條的色彩
self.LineColor=[UIColor blueColor];
[self.btnArr removeAllObjects];
[self setNeedsDisplay];
});
}];
NSLog(@"暗碼毛病");
}
self.userInteractionEnabled=YES;
}
署理剖斷暗碼並完成跳轉
-(BOOL)blockView:(MYblockView *)blockView finishedWithPassword:(NSString *)password
{
if ([password isEqualToString:@"012"]) {
UIViewController *two=[UIViewController new];
two.view.backgroundColor=[UIColor greenColor];
[self.navigationController pushViewController:two animated:YES];
return YES;
}
else{
return NO;
}
}
最初設置掌握器navigationbar屬性
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
[ self.navigationController.navigationBar setTitleTextAttributes:@{
NSForegroundColorAttributeName :[UIColor whiteColor]
}];
以上就是本文的全體內容,願望對年夜家的進修有所贊助。
【iOS完成手勢解鎖操作】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!