iOS實現(xiàn)九宮格連線手勢解鎖
本文實例為大家分享了iOS實現(xiàn)九宮格連線手勢解鎖的具體代碼,供大家參考,具體內(nèi)容如下
Demo下載地址:手勢解鎖
效果圖:

核心代碼:
//
// ClockView.m
// 手勢解鎖
//
// Created by llkj on 2017/8/24.
// Copyright © 2017年 LayneCheung. All rights reserved.
//
#import "ClockView.h"
@interface ClockView ()
//存放當(dāng)前選中的按鈕
@property (nonatomic, strong) NSMutableArray *selectBtnArry;
//當(dāng)前手指所在點
@property (nonatomic, assign) CGPoint curP;
@end
@implementation ClockView
- (void)awakeFromNib{
[super awakeFromNib];
//初始化
[self setUp];
}
- (NSMutableArray *)selectBtnArry{
if (_selectBtnArry == nil) {
_selectBtnArry = [NSMutableArray array];
}
return _selectBtnArry;
}
- (void)setUp{
for (int i = 0; i < 9; i ++) {
//創(chuàng)建按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = i;
btn.userInteractionEnabled = NO;
[btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"gesture_node_selected"] forState:UIControlStateSelected];
[self addSubview:btn];
}
}
//獲取當(dāng)前點
- (CGPoint)getCurrentPoint:(NSSet *)point{
UITouch *touch = [point anyObject];
return [touch locationInView:self];
}
//返回按鈕
- (UIButton *)btnRectContainsPoint:(CGPoint)point{
//遍歷brn判斷當(dāng)前點在不在btn上
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, point)) {
return btn;
}
}
return nil;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1.獲取當(dāng)前點
CGPoint curP = [self getCurrentPoint:touches];
//2.判斷當(dāng)前點在不在btn上
UIButton *btn = [self btnRectContainsPoint:curP];
if (btn && btn.selected == NO) {
btn.selected = YES;
//保存選中的按鈕
[self.selectBtnArry addObject:btn];
}
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1.獲取當(dāng)前點
CGPoint curP = [self getCurrentPoint:touches];
self.curP = curP;
//2.判斷當(dāng)前點在不在btn上
UIButton *btn = [self btnRectContainsPoint:curP];
if (btn && btn.selected == NO) {
btn.selected = YES;
//保存選中的按鈕
[self.selectBtnArry addObject:btn];
}
//重繪
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSMutableString *str = [NSMutableString string];
//1.取消所有選中的按鈕
for (UIButton *btn in self.selectBtnArry) {
btn.selected = NO;
[str appendFormat:@"%ld", btn.tag];
}
//2.清空路徑
[self.selectBtnArry removeAllObjects];
[self setNeedsDisplay];
//查看是否是第一次設(shè)置密碼
NSString *keyPwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"keyPwd"];
if (!keyPwd) {
[[NSUserDefaults standardUserDefaults] setObject:str forKey:@"keyPwd"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"第一次設(shè)置密碼成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertV show];
NSLog(@"第一次輸入密碼");
}else{
if ([keyPwd isEqualToString:str]) {
NSLog(@"密碼正確");
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手勢輸入正確" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertV show];
}else{
NSLog(@"密碼錯誤");
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手勢輸入錯誤" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertV show];
}
}
//3.查看當(dāng)前選中按鈕的順序
NSLog(@"選中按鈕順序為:%@",str);
}
- (void)drawRect:(CGRect)rect{
if (self.selectBtnArry.count) {
//1.創(chuàng)建路徑
UIBezierPath *path = [UIBezierPath bezierPath];
//2.取出所有保存的按鈕
for (int i = 0; i < self.selectBtnArry.count; i ++) {
UIButton *btn = self.selectBtnArry[i];
//當(dāng)前按鈕是不是第一個按鈕
if (i == 0) {
//設(shè)置成路徑的起點
[path moveToPoint:btn.center];
} else {
//添加一根線到按鈕中心
[path addLineToPoint:btn.center];
}
}
//添加一根線到當(dāng)前手指所在點
[path addLineToPoint:self.curP];
//設(shè)置線寬/顏色
[path setLineWidth:5];
[[UIColor whiteColor] set];
[path setLineJoinStyle:kCGLineJoinRound];
//3.繪制路徑
[path stroke];
}
}
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat x = 0;
CGFloat y = 0;
CGFloat btnWH = 75;
int column = 3;
int margin = (self.bounds.size.width - (column * btnWH)) / (column + 1);
int currentColumn = 0;
int currentRow = 0;
for (int i = 0; i < self.subviews.count; i ++) {
// 求當(dāng)前所在的列
currentColumn = i % column;
// 求當(dāng)前所在的行
currentRow = i / column;
x = margin + (btnWH + margin) * currentColumn;
y = margin + (btnWH + margin) * currentRow;
UIButton *btn = self.subviews[i];
btn.frame = CGRectMake(x, y, btnWH, btnWH);
}
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)之UITableView左滑刪除等自定義功能
今天來給大家介紹下iOS開發(fā)中UITableView左滑實現(xiàn)微信中置頂,刪除等功能。對大家開發(fā)iOS具有一定的參考借鑒價值,有需要的朋友們一起來看看吧。2016-09-09
iOS應(yīng)用開發(fā)中使用設(shè)計模式中的觀察者模式的實例
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用設(shè)計模式中的觀察者模式的實例,包括Cocoa框架使用中的KVO機制的相關(guān)配合運用,代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
VIVO手機上del鍵無效OnKeyListener不響應(yīng)的原因及解決方法
最近有用戶反饋VIVO手機上回出現(xiàn),Del鍵無效的問題,最后找到問題所在是EdiText的OnKeyListener沒有響應(yīng),下面通過本文給大家分享下解決方案2016-12-12
IOS self和super詳解實現(xiàn)原理及區(qū)別
這篇文章主要介紹了iOS self和super詳解實現(xiàn)原理及區(qū)別的相關(guān)資料,這里不僅說明區(qū)別并介紹實現(xiàn)原理,具有參考價值,需要的朋友可以參考下2016-12-12
iOS開發(fā)之Objective-c的Runtime理解指南
這篇文章主要介紹了iOS開發(fā)之Objective-c的Runtime理解指南的相關(guān)資料,需要的朋友可以參考下2022-08-08
iOS基于UITableView實現(xiàn)多層展開與收起
這篇文章主要為大家詳細(xì)介紹了iOS基于UITableView實現(xiàn)多層展開與收起的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Drawer?Builder組件實現(xiàn)flutter側(cè)邊抽屜效果示例分析
這篇文章主要為大家介紹了Drawer?Builder組件實現(xiàn)flutter側(cè)邊抽屜效果示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

