iOS开发_手势解锁

创建一个继承于UIView的子类如下:

头文件ZRLockView.h

 1 ZRLockView.h
 2
 3 #import <UIKit/UIKit.h>
 4
 5 @class ZRLockView;
 6
 7 @protocol ZRLockViewDelegate <NSObject>
 8
 9 @optional
10 -(void)lockView:(ZRLockView *)lockView didFinishPath:(NSString *)path;
11
12 @end
13
14 @interface ZRLockView : UIView
15
16 @property(nonatomic,weak) IBOutlet id<ZRLockViewDelegate> delegate;
17
18 @end

ZRLockView.m文件

  1 ZRLockView.m
  2
  3 #import "ZRLockView.h"
  4
  5 @interface ZRLockView ()
  6
  7 @property(nonatomic,strong) NSMutableArray *selectedButtons;
  8 @property(nonatomic,assign) CGPoint currentMovePoint;
  9 @end
 10
 11 @implementation ZRLockView
 12
 13 #pragma mark --初始化
 14 -(NSMutableArray *)selectedButtons{
 15     if (!_selectedButtons) {
 16         _selectedButtons = [NSMutableArray array];
 17     }
 18     return _selectedButtons;
 19 }
 20
 21 -(id)initWithFrame:(CGRect)frame{
 22     self = [super initWithFrame:frame];
 23     if (self) {
 24         [self setUp];
 25     }
 26     return self;
 27 }
 28
 29 -(id)initWithCoder:(NSCoder *)aDecoder{
 30     if (self == [super initWithCoder:aDecoder]) {
 31         [self setUp];
 32     }
 33     return self;
 34 }
 35 //初始化
 36 -(void)setUp{
 37     for (int index=0; index<9; index++) {
 38         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 39         btn.userInteractionEnabled = NO;
 40         [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
 41         [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
 42         btn.tag = index;
 43         [self addSubview:btn];
 44     }
 45 }
 46
 47 -(void)layoutSubviews{
 48
 49     [super layoutSubviews];
 50     for (int index=0; index<self.subviews.count; index++) {
 51         //取出按钮
 52         UIButton *btn = self.subviews[index];
 53         // 设置frame
 54         CGFloat btnW = 74;
 55         CGFloat btnH = 74;
 56         int totalColumns = 3;
 57         int col = index % totalColumns;
 58         int row = index / totalColumns;
 59         CGFloat marginX = (self.frame.size.width -totalColumns * btnW)/(totalColumns + 1);
 60         CGFloat marginY = marginX;
 61
 62         CGFloat btnX = marginX + col * (btnW + marginX);
 63         CGFloat btnY = marginY + row *(btnH + marginY);
 64         btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
 65     }
 66 }
 67
 68 #pragma mark --私有方法,根据touches集合获得对应的触摸点位置
 69 -(CGPoint)pointWithTouches:(NSSet *)touches{
 70     UITouch *touch = [touches anyObject];
 71     return [touch locationInView:touch.view];
 72 }
 73 //根据触摸点获得对应的按钮
 74 -(UIButton *)buttonWithPoint:(CGPoint)point{
 75     for (UIButton *btn in self.subviews) {
 76         CGFloat wh = 30;
 77         CGFloat btnX = btn.center.x - wh * 0.5;
 78         CGFloat btnY = btn.center.y - wh * 0.5;
 79         if (CGRectContainsPoint(CGRectMake(btnX, btnY, wh, wh), point)) {
 80             return btn;
 81         }
 82     }
 83     return nil;
 84 }
 85 #pragma mark --触摸方法
 86 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 87     //清空当前点
 88     self.currentMovePoint = CGPointMake(-10, -10);
 89     //获得触摸点
 90     CGPoint pos = [self pointWithTouches:touches];
 91     // 获得触摸按钮
 92     UIButton *btn = [self buttonWithPoint:pos];
 93     //3 设置状态
 94     if (btn && [self.selectedButtons containsObject:btn] == NO) {
 95         btn.selected = YES;
 96         [self.selectedButtons addObject:btn];
 97     }
 98     //4 刷新
 99     [self setNeedsDisplay];
100 }
101
102 -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
103     //获得触摸点
104     CGPoint pos = [self pointWithTouches:touches];
105     // 获得触摸按钮
106     UIButton *btn = [self buttonWithPoint:pos];
107     if (btn && btn.selected ==NO) {//摸到按钮
108         btn.selected = YES;
109         [self.selectedButtons addObject:btn];
110     }else{//没有摸到按钮
111         self.currentMovePoint = pos;
112     }
113     //4 刷新
114     [self setNeedsDisplay];
115 }
116
117 -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
118     //代理通知
119     if ([self.delegate respondsToSelector:@selector(lockView:didFinishPath:)]) {
120         NSMutableString *path = [NSMutableString string];
121         for (UIButton *btn in self.selectedButtons) {
122             [path appendFormat:@"%d",btn.tag];
123         }
124         [self.delegate lockView:self didFinishPath:path];
125     }
126
127 //    NSLog(@"%@",str);
128     //取消所选中的按钮
129 //    for (UIButton *btn in self.selectedButtons) {
130 //        btn.selected = NO;
131 //    }
132     [self.selectedButtons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
133     //清空选中的按钮
134     [self.selectedButtons removeAllObjects];
135     [self setNeedsDisplay];
136
137     self.currentMovePoint = CGPointZero;
138 }
139
140 -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
141     [self touchesEnded:touches withEvent:event];
142 }
143
144 -(void)drawRect:(CGRect)rect{
145     //
146     if (self.selectedButtons.count == 0) return;
147
148     UIBezierPath *path = [UIBezierPath bezierPath];
149     //便利所有的按钮
150     for (int index = 0; index < self.selectedButtons.count; index++) {
151         UIButton *btn = self.selectedButtons[index];
152         if (index == 0) {
153             [path moveToPoint:btn.center];
154         }else{
155             [path addLineToPoint:btn.center];
156         }
157     }
158     //链接
159     if (CGPointEqualToPoint(self.currentMovePoint, CGPointMake(-10, -10)) == NO) {
160         [path addLineToPoint:self.currentMovePoint];
161     }
162     //绘图
163     path.lineWidth = 8;
164     path.lineJoinStyle = kCGLineJoinBevel;
165 //    [[UIColor greenColor] set];
166     [[UIColor colorWithRed:32/255.0 green:210/255.0 blue:254/255.0 alpha:0.7] set];
167     [path stroke];
168 }
169
170 @end

将我写好的类文件直接拖到Xcode已有的项目中,想在哪个类中实现这个功能,导入头文件,运行你就懂了

 1 #import "ViewController.h"
 2 #import "ZRLockView.h"
 3
 4 @interface ViewController ()<ZRLockViewDelegate>
 5
 6 @end
 7
 8 @implementation ViewController
 9
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view, typically from a nib.
13 }
14
15 - (void)lockView:(ZRLockView *)lockView didFinishPath:(NSString *)path{
16     NSLog(@"获得用户的手势路径:%@",path);
17 }
18
19 @end
时间: 2024-10-12 05:02:30

iOS开发_手势解锁的相关文章

iOS开发之手势解锁

本文主要介绍通过手势识别实现手势解锁功能,这个方法被广泛用于手机解锁,密码验证,快捷支付等功能实现.事例效果如下所示. 首先,我们先分析功能的实现过程,首先我们需要先看大致的实现过程: 1.加载九宫格页面 2.实现按钮被点击及滑动过程中按钮状态的改变 3.实现滑动过程中的连线 4.绘制完毕后判定密码是否正确, 5.密码判定后实现跳转. 下面我们就来用代码实现上述五个过程. 1.加载九宫格界面 1.1九宫格内控件的分布 3*3 ,我们可以自定义view(包含3*3个按钮),添加到viewContr

iOS开发摇动手势实现详解

1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势.系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你快速摇动设备三次,那只会收到一个摇动事件. 2,想要实现摇动手势,首先需要使视图控制器成为第一响应者,注意不是单独的控件.成为第一响应者最恰当的时机是在视图出现的时候,而在视图消失的时候释放第一响应者. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -(BOOL)canBecomeFirstRespond

iOS开发之手势gesture详解(二)

与其他用户界面控件交互 UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用于手势识别重叠的默认动作的控制,其中包括: 一根手指单击动作:UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl. 一根手指擦碰动作:UISlider 一根手指拖动动作:UISwitch 包含多点触摸的事件 在iO

iOS开发之手势gesture详解(一)

前言 在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象,当发生手势,绑定到的view对象会响应,它确定这个动作是否对应一个特定的手势(swipe,pinch,pan,rotation).如果它能识别这个手势,那么就会向绑定它的view发送消息,如下图 UIKit框架提供了一些预定义的GestureRecognizer.包含下列手势 UITapGestu

IOS开发之手势—UIGestureRecognizer 共存

IOS开发之手势--UIGestureRecognizer 共存 在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断.用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹.UIGestureRecognizer的衍生类別有以下几种: UITapGestureRec

微信小程序开发教程-手势解锁

手势解锁是app上常见的解锁方式,相比输入密码方式操作起来要方便许多.下面展示如何基于微信小程序实现手机解锁.最终实现效果如下图: 整个功能基于canvas实现,首先添加画布组件,并设定样式 <!--index.wxml--> <view class="container"> <canvas canvas-id="id-gesture-lock" class="gesture-lock" bindtouchstart

iOS开发_初识视频直播

一.使用第三方ijkPlayer框架开发直播 1.去到B站得github主页,找到ijkplayer项目,下载源码 ijkplayer下载地址 2.Demo的使用以及如何编译Demo 请移步如何快速的开发一个完整的iOS直播app(播放篇) 其实里面讲的很详细,我也是参照这个写的Demo.然后在他的基础上加了一点东西.(不废话我直接上我的代码) 先看看效果图 特点 1.纯代码Masonry布局 2.集成ijkplayer第三方库,实现拉流播放 3.打包ijkplayer静态库,实现release

M牛C原创博客——IOS开发中手势滑动的6种技巧

手势的种类 所有手势的父类:UIGestureRecognizer 6种手势:UI XXX GestureRecognizer UITapGestureRecognizer  点击一下屏幕 UISwipeGestureRecognizer  轻扫屏幕,如解锁 UILongPressGestureRecognizer 长按手势 UIPinchGestureRecognizer  捏合手势 UIPanGestureRecognizer 移动手势 UIRotationGestureRecognizer

IOS开发之手势——UIGestureRecognizer 共存

在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断.用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹.UIGestureRecognizer的衍生类別有以下几种: UITapGestureRecognizer UIPinchGestureRecognizer