溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS如何實(shí)現(xiàn)九宮格連線手勢解鎖

發(fā)布時(shí)間:2020-08-03 09:16:51 來源:億速云 閱讀:381 作者:小豬 欄目:移動開發(fā)

這篇文章主要講解了iOS如何實(shí)現(xiàn)九宮格連線手勢解鎖,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

效果圖:

iOS如何實(shí)現(xiàn)九宮格連線手勢解鎖

核心代碼:

//
// 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)前手指所在點(diǎn)
@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)前點(diǎn)
- (CGPoint)getCurrentPoint:(NSSet *)point{

 UITouch *touch = [point anyObject];
 return [touch locationInView:self];

}

//返回按鈕
- (UIButton *)btnRectContainsPoint:(CGPoint)point{

 //遍歷brn判斷當(dāng)前點(diǎn)在不在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)前點(diǎn)
 CGPoint curP = [self getCurrentPoint:touches];

 //2.判斷當(dāng)前點(diǎn)在不在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)前點(diǎn)
 CGPoint curP = [self getCurrentPoint:touches];
 self.curP = curP;

 //2.判斷當(dāng)前點(diǎn)在不在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(@"密碼錯(cuò)誤");
  UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"手勢輸入錯(cuò)誤" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
  [alertV show];
 }
 }
 //3.查看當(dāng)前選中按鈕的順序
 NSLog(@"選中按鈕順序?yàn)?%@",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)前按鈕是不是第一個(gè)按鈕
  if (i == 0) {
  //設(shè)置成路徑的起點(diǎn)
  [path moveToPoint:btn.center];
  } else {
  //添加一根線到按鈕中心
  [path addLineToPoint:btn.center];
  }
 }

 //添加一根線到當(dāng)前手指所在點(diǎn)
 [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)容,是不是對iOS如何實(shí)現(xiàn)九宮格連線手勢解鎖有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI