2016-1-10 手势解锁demo的实现

一:实现自定义view,在.h,.m文件中代码如下:

#import <UIKit/UIKit.h>
@class ZLLockView;
@protocol ZLLockViewDelegate <NSObject>
- (void)lockView:(ZLLockView *)lockView didSelectedPwd: (NSString *)pwd;
@end
@interface ZLLockView : UIView
@property (nonatomic, weak) id<ZLLockViewDelegate> delegate;

@end
//
//  ZLLockView.m
//  手势解锁demo实现
//
//  Created by Mac on 16/1/9.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ZLLockView.h"
@interface ZLLockView()
@property (nonatomic, strong) NSMutableArray *btnsSelected;

@property (nonatomic, assign) CGPoint lastPoint;

@end
@implementation ZLLockView
- (NSMutableArray *)btnsSelected
{
    if (!_btnsSelected) {
        _btnsSelected = [NSMutableArray array];
    }
    return _btnsSelected;
}
- (instancetype)init
{
    if (self = [super init]) {
        [self setBtn];
    }
    return self;
}
- (void)setBtn
{
    for (int i = 0; i < 9; i ++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i;

        [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        btn.userInteractionEnabled = NO;
        [self addSubview:btn];
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 7;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [[UIColor blueColor] set];
    NSInteger count = self.btnsSelected.count;
    if (count ==0)return;
        for (NSInteger i = 0; i < count; i ++) {
        UIButton *btn = self.btnsSelected[i];
        if (i == 0) {
            [path moveToPoint:btn.center];
        }else{
            [path addLineToPoint:btn.center];
        }
    }
    [path addLineToPoint:self.lastPoint];
    [path stroke];
}

- (void)layoutSubviews
{
    CGFloat btnW = 74;
    CGFloat btnH = 74;
//    间距
    CGFloat padding = (self.frame.size.width - btnW * 3) / 4;
    NSInteger count = self.subviews.count;
    for (NSInteger i = 0; i < count; i ++) {
        UIButton *btn =  self.subviews[i];
//        当前按钮所处的列
        NSInteger column = i % 3;
//        计算btn的x值
        CGFloat btnX = (column+1) * padding + column * btnW;
//        当前按钮所处的行
        CGFloat row = i / 3;
        CGFloat btnY = (row+1) * padding + row * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    获取当前点
    UITouch *touch = [touches anyObject];
    CGPoint location =  [touch locationInView:touch.view];
  //    判断在不在范围内
    for (UIButton *btn  in self.subviews) {
        if (CGRectContainsPoint(btn.frame, location)) {//判断获得的点在不在范围内
            //将选中的按钮放在数组里
            if (btn.selected == NO) {
                [self.btnsSelected addObject:btn];
            }
            [btn setSelected:YES];
        }
        else self.lastPoint = location;
    }
    [self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSMutableString *pwd = [NSMutableString string];

    for (UIButton *btn in self.btnsSelected) {
        [btn setSelected:NO];
            //拼接选中按钮的索引
            [pwd appendFormat:@"%ld",btn.tag];
    [self setNeedsDisplay];
    }
    [self.btnsSelected removeAllObjects];
    NSLog(@"%@",pwd);
    [self.delegate lockView:self didSelectedPwd:pwd];
}
@end

二:在控制器中实现代理方法,代码如下:

//
//  ViewController.m
//  手势解锁demo实现
//
//  Created by Mac on 16/1/9.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ViewController.h"
#import "ZLLockView.h"
#import "MBProgressHUD+CZ.h"
@interface ViewController ()<ZLLockViewDelegate>
@property (nonatomic, strong) ZLLockView *lockView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_refresh_bg"]];
    // Do any additional setup after loading the view, typically from a nib.
    ZLLockView *lockView = [[ZLLockView alloc] init];
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    lockView.frame = CGRectMake(0, 0, screenW, screenW);
    lockView.center = self.view.center;
    lockView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:lockView];
    self.lockView = lockView;
    lockView.delegate = self;

}
- (void) lockView:(ZLLockView *)lockView didSelectedPwd:(NSString *)pwd
{
    if ([pwd isEqual:@"012345678"]) {
        [MBProgressHUD showMessage:@"密码正确!"];
        [self.lockView removeFromSuperview];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
    }else{
        [MBProgressHUD showMessage:@"密码错误!"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
        });
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

三:效果:

时间: 2024-11-04 06:19:45

2016-1-10 手势解锁demo的实现的相关文章

[iOS UI进阶 - 5.0] 手势解锁Demo

A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B.实现 使用按钮来处理每个圆点 使用代码生成按钮 取消按钮点击事件 设置普通状态和选中状态的背景图片 CGRectContainsPoint,移动到按钮范围内改变按钮为选中状态 按钮的连接:使用数组存储被选中的所有按钮,画上连线 已经连线的按钮不需要再连线 触摸结束清空连线和按钮选中状态 移动中也要画出线,最后

【iOS开发之旅】手势解锁

BOERLockView.h // // BOERLockView.h // BoerScore // // Created by ChenQianPing on 16/2/18. // Copyright © 2016年 boer. All rights reserved. // #import <UIKit/UIKit.h> @class BOERLockView; @protocol BOERLockViewDelegate <NSObject> // 结束手势解锁代理事件

iOS开发之手势解锁

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

(素材源码)猫猫学IOS(三十五)UI之Quartz2D仿真支付宝手势解锁_代理获得密码。

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 源码:http://download.csdn.net/detail/u013357243/8669765 效果: 代码: NYLockView.h // // NYLockView.h // 手势解锁 // // Created by apple on 15-5-6. // Copyright (c) 2015年 znyca

手势解锁

ViewController.m // //  ViewController.m //  6A08.手势解锁 // //  Created by huan on 16/2/1. //  Copyright © 2016年 huanxi. All rights reserved. // #import "ViewController.h" #import "CZLockView.h" @interface ViewController () @end @impleme

手势解锁步骤的基本思路

------------- 基本思路 -------------- - 1. 搭建界面,九宫格算法 - 2. 处理按钮选中状态 - 3. 按钮之间画线 - 4. 手指和按钮之间画线 - 5. 判断解锁密码是否正确 ------------- 基本思路 -------------- 1. 拖拽图片素材 2. 设置控制器 view 的背景色为"HomeButtomBG"图片平铺后的效果 self.view.backgroundColor = [UIColor colorWithPatter

UI进阶--Quartz2D和触摸事件的简单使用:手势解锁

需求:实现一个简易的手势解锁应用,具体效果如下图所示: 实现步骤: 1.代码创建界面,自定义一个view,设置view的背景,颜色等属性: 2.在自定义的view中,定义2个属性,一个是存储被选中按钮的可变数组,另外一个是最后的触摸点(CGPoint); 3.重写initWithFrame方法,在这里,自定义一个方法给initWithFrame方法调用即可,这个自定义的方法里,初始化9个按钮,设置每个按钮的tag,正常状态下的图片以及选中状态的图片,并设置和用户的交互为NO: 4.在自定义的vi

手势解锁的实现思路

------------- 基本思路 -------------- - 1. 搭建界面,九宫格算法 - 2. 处理按钮选中状态 - 3. 按钮之间画线 - 4. 手指和按钮之间画线 - 5. 判断解锁密码是否正确 ------------- 基本思路 -------------- 1. 拖拽图片素材 2. 设置控制器 view 的背景色为"HomeButtomBG"图片平铺后的效果 self.view.backgroundColor = [UIColor colorWithPatter

iOS开发UI篇—实现一个简单的手势解锁应用(基本)

iOS开发UI篇—实现一个简单的手势解锁应用(基本) 一.实现效果 实现效果图: 二.手势解锁应用分析 1.监听手指在view上的移动,首先肯定需要自定义一个view,重写touch began,touch move等方法,当手指移动到圈上时,让其变亮.可以通过button按钮来实现. 2.界面搭建 背景图片(给控制器的view添加一个imageview,设置属性背景图片) 九个按钮(把九个按钮作为一个整体,使用一个大的view来管理这些小的view,这些小的view就是9个button.如果使