手勢解鎖
這是我模仿的QQ手勢解鎖部分界面~
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DN2UTOxQDNyEDOxgDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
密碼輸入錯誤效果
密碼輸入正确效果
界面粗糙,還請海涵~
接下來開始上代碼了~
我是自定義了一個SpeedDailView繼承于UIView的九宮格界面
解釋都在注釋裡了
SpeedDialView.h
#import <UIKit/UIKit.h>
@interface SpeedDialView : UIView
//用來盛放選中的button
@property (nonatomic,retain) NSMutableArray *btns;
//輸入的密碼
@property (nonatomic,strong) NSMutableString *code;
//正确密碼
@property (nonatomic,strong) NSString *password;
//觸摸是否結束
@property (nonatomic,assign) BOOL isEnd;
@end
SpeedDialView.m
#import "SpeedDialView.h"
@implementation SpeedDialView
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
//懶加載
- (NSMutableArray *)btns
{
if (!_btns) {
self.btns = [[NSMutableArray alloc]init];
}
return _btns;
}
//建立9個按鈕
- (void)setUpBasicUI
{
for (int i = 0; i < 9; i++) {
UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
btn.tag = i;
[self addSubview:btn];
}
}
//設定9個按鈕的位置
- (void)layoutSubviews
{
[super layoutSubviews];
//這個方法隻運作一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self setUpBasicUI];
});
//經典的9宮格算法
int totalColume = 3;
CGFloat bWidth = 74;
CGFloat bHeight = bWidth;
CGFloat margin = (self.frame.size.width - bWidth * totalColume)/(totalColume + 1);
for (int i = 0; i< self.subviews.count; i++) {
int currentRow = i/totalColume;
int currentColumn = i%totalColume;
CGFloat originX = margin + (currentColumn * (margin + bWidth));
CGFloat originY = currentRow * (margin + bHeight);
//設定Button的外觀和大小
UIButton *btn = self.subviews[i];
[btn setImage:[UIImage imageNamed:@"xv"] forState:(UIControlStateNormal)];
[btn setImage:[UIImage imageNamed:@"shi"] forState:(UIControlStateSelected)];
btn.contentMode = UIViewContentModeCenter;
btn.userInteractionEnabled = false;
btn.frame = CGRectMake(originX, originY, bWidth, bHeight);
}
}
//擷取目前的觸摸點的坐标
- (CGPoint)pointWithTouches:(NSSet<UITouch *> *)touches
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
return point;
}
//判斷目前點在不在Button中
- (UIButton *)buttonWithPoint:(CGPoint)point
{
for (int i = 0; i < self.subviews.count; i++) {
UIButton *btn = self.subviews[i];
CGFloat wh = btn.frame.size.width - 5;
//找到Button中心附近25*25的面積
CGFloat frameX = btn.center.x - wh * 0.5;
CGFloat frameY = btn.center.y - wh * 0.5;
//判斷這個點在button中心附近25*25的面積内的話就是它了
if (CGRectContainsPoint(CGRectMake(frameX, frameY, wh, wh), point)) {
return btn;
}
}
return nil;
}
//設定Button被選中的狀态
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//當觸摸開始的時候将之前的痕迹抹掉 重新開始
self.code = [[NSMutableString alloc]init];
self.isEnd = NO;
for (int i = 0; i < self.btns.count; i ++) {
UIButton *btn = self.btns[i];
btn.selected = NO;
}
[self.btns removeAllObjects];
//找到touch到的點的位置(此處隻有一個點 就是開始的那個點)
CGPoint point = [self pointWithTouches:touches];
//判斷這個點所在的Button
UIButton *btn = [self buttonWithPoint:point];
if (btn != nil && btn.selected == false) {
btn.selected = YES;
//把button加到數組中
[self.btns addObject:btn];
//并記錄走過的路線 用Button的tag拼接出的字元串
[self.code appendString:[NSString stringWithFormat:@"%ld",(long)btn.tag]];
}
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//找到touch到的點的位置
CGPoint point = [self pointWithTouches:touches];
//判斷這個點所在的Button
UIButton *btn = [self buttonWithPoint:point];
if (btn != nil && btn.selected == false) {
btn.selected = YES;
//把button加到數組中
[self.btns addObject:btn];
//并記錄走過的路線 用Button的tag拼接出的字元串
[self.code appendString:[NSString stringWithFormat:@"%ld",(long)btn.tag]];
}
[self setNeedsDisplay];
}
//使用touchend清空資料
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//繪制選中的按鈕少于3個的時候沒有軌迹記錄
if (self.btns.count < 3) {
for (int i = 0; i < self.btns.count; i ++) {
UIButton *btn = self.btns[i];
btn.selected = NO;
}
[self.btns removeAllObjects];
}
else
{
//标志 結束
self.isEnd = YES;
}
[self setNeedsDisplay];
}
//優化 先判空
-(void)drawRect:(CGRect)rect
{
//如果沒有選擇的button 則不繪圖
if (self.btns.count == 0) {
return;
}
//使用貝塞爾曲線繪制路線
UIBezierPath * path = [UIBezierPath bezierPath];
for (int i = 0; i < self.btns.count; i ++) {
UIButton *btn = self.btns[i];
if (i == 0) {
[path moveToPoint:btn.center];
}
else
{
[path addLineToPoint:btn.center];
}
}
//設定線的顔色
[[UIColor colorWithRed:23/255.0 green:171/255.0 blue:227/255.0 alpha:1] set];
//設定線的寬度
[path setLineWidth:3];
//設定連接配接風格
[path setLineJoinStyle:(kCGLineJoinRound)];
//判斷是否結束 在touchEnd方法中更改isEnd的值
if (_isEnd == YES) {
if (![self.code isEqualToString:self.password]) {
//修改Path的顔色
[[UIColor redColor]set];
//發送通知 提醒ViewController更改Label上的文字
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabel" object:nil];
}
else
{
//發送通知 提醒ViewController更改Label上的文字
[[NSNotificationCenter defaultCenter]postNotificationName:@"changeLabel2" object:nil];
}
}
//渲染
[path stroke];
}
@end
使用方法
在ViewController的Xib裡設計了界面,其中最下面正方形是關聯SpeedDialView的View。
ViewController.m
<span style="font-size:18px;">@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet SpeedDialView *speedDialView;
@end
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated
{
self.label.text = @"請輸入手勢密碼";
self.label.textColor = [UIColor darkGrayColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabel) name:@"changeLabel" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabel2) name:@"changeLabel2" object:nil];
self.speedDialView.password = @"1345";
}
- (void)changeLabel
{
self.label.text = @"密碼輸入錯誤";
self.label.textColor = [UIColor redColor];
}
- (void)changeLabel2
{
self.label.text = @"恭喜,密碼輸入正确";
self.label.textColor = [UIColor greenColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
</span>
最後,就是成果展示了~
大功告成~
我對明天沒有期待,因為我對今天滿意~
UIButton圖檔提供
最後,提供Demo位址點選打開連結
我是寫的OC版的,感謝簡書作者王鑫20111Swift版傳送門的無私分享~
實踐所寫,如有錯誤,還請指正,謝謝!