查看: 933|回复: 0

[其他分享] iOS仿邮箱大师的九宫格手势密码解锁


16-5-20 14:41:30 | [复制链接]

69

主题

254

帖子

556

积分

大牛

Rank: 4

发表于 16-5-20 14:41:30 | 显示全部楼层 |阅读模式
  iOS仿邮箱大师的九宫格手势密码解锁


  本文实例为大家分享了iOS手势密码解锁的相关代码,供大家参考,具体内容如下:


  1. //
  2. // LockView.m
  3. // 手势解锁
  4. //
  5. // Created by Daniel on 16/4/4.
  6. // Copyright ? 2016年 Daniel. All rights reserved.
  7. //
  8. #import "LockView.h"
  9. @interface LockView ()
  10. /** 保存已选中的按钮 */
  11. @property(nonatomic, strong) NSMutableArray *selectedBtn;
  12. /** 当前触摸点 */
  13. @property(nonatomic, assign) CGPoint curp;
  14. @end
  15. @implementation LockView
  16. - (NSMutableArray *)selectedBtn {
  17. if (_selectedBtn == nil) {
  18. _selectedBtn = [NSMutableArray array];
  19. }
  20. return _selectedBtn;
  21. }
  22. - (IBAction)pan:(UIPanGestureRecognizer *)pan {
  23. //获取当前触摸点
  24. _curp = [pan locationInView:self];
  25. //判断触摸点在不在按钮上
  26. for (UIButton *btn in self.subviews) {
  27. //如果在按钮上就设置选中状态
  28. //触摸点必须在button中心点30范围内才选中,更精确,体验更好
  29. CGRect rect = CGRectMake(btn.center.x, btn.center.y, 30, 30);
  30. if (CGRectContainsPoint(rect, _curp) && btn.selected == NO) {
  31. btn.selected = YES;
  32. //将这个选中的按钮保存起来
  33. [self.selectedBtn addObject:btn];
  34. }
  35. }
  36. //重绘,调用drawRect方法
  37. [self setNeedsDisplay];
  38. //手指抬起时
  39. if(pan.state == UIGestureRecognizerStateEnded) {
  40. NSMutableString *strM = [NSMutableString string];
  41. //如果当前触摸点不在Button上,则手指抬起时,就显示选中的Button以及连线
  42. UIButton *lastBtn = [self.selectedBtn lastObject];
  43. //把最后一个选中按钮的中心点设置为当前触摸点,清除最后多出来的一截连线
  44. _curp = lastBtn.center;
  45. for (UIButton *btn in self.selectedBtn) {
  46. //保存手势密码
  47. [strM appendFormat:@"%ld", btn.tag];
  48. }
  49. //strM就是密码了
  50. NSLog(@"%@",strM);
  51. //TODO:对比之前保存的密码,如果对了就直接跳转界面了
  52. //NSLog(@"手指抬起");
  53. //这里了执行完之后系统自动调用了重绘方法
  54. //等待2s后清除所有连线,清除Button的选中状态
  55. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  56. //取消按钮选中
  57. //[self.selectedBtn makeObjectsPerformSelector:@selector(setSelected:) withObject:NO];这句不知道为啥没有效果,只好用循环了
  58. for (UIButton *btn in self.selectedBtn) {
  59. [btn setSelected:NO];
  60. }
  61. //清除连线,清空选中按钮
  62. [self.selectedBtn removeAllObjects];
  63. [self setNeedsDisplay];
  64. });
  65. }
  66. }
  67. - (void)drawRect:(CGRect)rect {
  68. NSInteger count = self.selectedBtn.count;
  69. //如果没有按钮被选中,就不画线
  70. if (count == 0) {
  71. return;
  72. }
  73. UIBezierPath *path = [UIBezierPath bezierPath];
  74. //把所有点都连完线
  75. for (int i = 0; i < count; i++) {
  76. UIButton * btn = self.selectedBtn[i];
  77. if (i == 0) {
  78. //如果是第一个点,就设置为起点
  79. [path moveToPoint:btn.center];
  80. }else {
  81. //添加连线
  82. [path addLineToPoint: btn.center];
  83. }
  84. }
  85. //画最后一个点到手指触摸点之间的线
  86. [path addLineToPoint:_curp];
  87. [[UIColor greenColor]set];
  88. path.lineJoinStyle = kCGLineJoinRound;
  89. path.lineWidth = 8;
  90. [path stroke];
  91. }
  92. - (void)awakeFromNib {
  93. //创建9个按钮
  94. for (int i = 0; i < 9; i++) {
  95. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  96. //不允许与用户交互,也就是点击事件不作处理
  97. btn.userInteractionEnabled = NO;
  98. btn.tag = i;
  99. [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
  100. [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
  101. [self addSubview:btn];
  102. }
  103. }
  104. - (void)layoutSubviews {
  105. [super layoutSubviews];
  106. //布局子控件
  107. NSInteger count = self.subviews.count;
  108. //列数
  109. int cols = 3;
  110. CGFloat x = 0;
  111. CGFloat y = 0;
  112. CGFloat w = 74;
  113. CGFloat h = 74;
  114. //间距
  115. CGFloat margin = (self.bounds.size.width - w * cols) / (cols + 1);
  116. CGFloat col = 0;
  117. CGFloat row = 0;
  118. for (NSInteger i = 0; i < count; i++) {
  119. UIButton *btn = self.subviews[i];
  120. //计算当前button的列行以及xy值
  121. col = i % cols;
  122. row = i / cols;
  123. x = margin + col * (margin + w);
  124. y = row * (margin + w);
  125. btn.frame = CGRectMake(x, y, w, h);
  126. }
  127. }
  128. @end
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

订阅|小黑屋|手机版|千锋教育论坛 ( 京ICP备12003911号-3 )

GMT+8, 20-4-9 12:26 , Processed in 0.296917 second(s), 44 queries .

Powered by Discuz! X3.2

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表