美文网首页iOS开发项目篇
密码解锁---改进(block,deldgate回调数据)

密码解锁---改进(block,deldgate回调数据)

作者: 小石头呢 | 来源:发表于2019-02-28 08:18 被阅读9次

源代码:

block方式:链接:
https://pan.baidu.com/s/1q8q7ES3a2FYLR7ZOC9MTIw 密码:iq0i
delegate方式:链接:
https://pan.baidu.com/s/1p84XRf6W0PwnWrKIPfB57A 密码:0o05

主要代码:

block方式

//
//  ViewController.m
//  PIN密码解锁-自定义(block)
//
//  Created by 许磊 on 2019/2/20.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import "ViewController.h"
#import "PINLockView.h"

@interface ViewController ()

/**PIN密码解锁---便于加载何种解锁方式*/
@property (nonatomic,strong) PINLockView *pinView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //创建
    self.pinView = [[PINLockView alloc] initWithFrame:self.view.bounds];
    //添加
    [self.view addSubview:_pinView];
    
    //避免循环引用
    __weak typeof(self) weakSelf = self;
    
    [_pinView setBlock:^(BOOL success) {
        
        //密码正确
        if (success == YES) {
            //显示主界面
            [UIView animateWithDuration:0.5 animations:^{
                //淡化
                weakSelf.pinView.alpha = 0.0;
            } completion:^(BOOL finished) {
                //移除
                [weakSelf.pinView removeFromSuperview];
            }];
        } else {//密码错误
            return ;
        }
    }];
    
}

@end


//
//  PINLockView.h
//  PIN密码解锁-自定义(block)
//
//  Created by 许磊 on 2019/2/20.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import <UIKit/UIKit.h>

//希望能被外部访问的数据

/**定义一个Block用于返回一个BOOL值*/
typedef void(^CompleteBlock) (BOOL success);

@interface PINLockView : UIView

//定义一个block属性变量
@property (nonatomic,copy) CompleteBlock block;

@end

//
//  PINLockView.m
//  PIN密码解锁-自定义(block)
//
//  Created by 许磊 on 2019/2/20.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import "PINLockView.h"

@interface PINLockView()<UITextFieldDelegate>

/**定义一个文本提示框*/
@property (nonatomic,strong) UILabel *hintLabel;

/**定义一个文本输入框*/
@property (nonatomic,strong) UITextField *inputTextField;

/**定义一个视图存放锁和提示*/
@property (nonatomic,strong) UIView *animatedView;

/**定义一个显示锁的视图*/
@property (nonatomic,strong) UIImageView *lockImageView;

/**定义一个显示提示的文本的视图*/
@property (nonatomic,strong) UILabel *placeholderLabel;

/**定义存储第一次设置密码的变量*/
@property (nonatomic,strong) NSString *firstPassword;

/**定义存储密码的变量*/
@property (nonatomic,strong) NSString *password;

@end

@implementation PINLockView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self != nil) {
        //创建显示图标
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-90)/2.0, 80, 90, 90)];
        //图片
        imageView.image = [UIImage imageNamed:@"icon"];
        //添加
        [self addSubview:imageView];
        
        //创建文本提示框
        self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, frame.size.width, 40)];
        //文本
        _hintLabel.text = @"欢迎";
        //字体颜色
        _hintLabel.textColor = [UIColor blackColor];
        //字体大小
        _hintLabel.font = [UIFont systemFontOfSize:20];
        //文本对齐方式
        _hintLabel.textAlignment = NSTextAlignmentCenter;
        //添加
        [self addSubview:_hintLabel];
        
        //获取系统自带的NSUserDefaults对象
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        //取数据 密码
        //[userDefaults setObject:@"" forKey:@"pin"];  //清空
        self.password = [userDefaults objectForKey:@"pin"];
        if (_password.length != 0) {
            _hintLabel.text = @"请输入密码";
        } else {
            _hintLabel.text = @"请设置密码";
        }
        
        
        //创建文本输入框
        self.inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 280, frame.size.width-20*2, 45)];
        //样式
        _inputTextField.borderStyle = UITextBorderStyleBezel;
        //边框颜色
        _inputTextField.layer.borderWidth = 1;
        _inputTextField.layer.borderColor = [UIColor blackColor].CGColor;
        //调试
        //_inputTextField.text = @"12345";
        //字体颜色
        _inputTextField.textColor = [UIColor blackColor];
        //字体大小
        _inputTextField.font = [UIFont systemFontOfSize:20];
        //文本对齐方式
        _inputTextField.textAlignment = NSTextAlignmentLeft;
        //设置光标颜色
        _inputTextField.tintColor = [UIColor clearColor];
        //消除按钮
        _inputTextField.clearButtonMode = YES;
        //安全输入
        _inputTextField.secureTextEntry = YES;
        //设置代理
        _inputTextField.delegate = self;
        //左边的视图 看不见
        _inputTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 1)];
        //左边视图的样式
        _inputTextField.leftViewMode = UITextFieldViewModeAlways;
        //添加
        [self addSubview:_inputTextField];
        
        //创建一个视图存放锁和提示
        self.animatedView = [[UIImageView alloc] initWithFrame:CGRectMake(28, 290, 61, 32)];
        //背景颜色
        _animatedView.backgroundColor = [UIColor whiteColor];
        //添加
        [self addSubview:_animatedView];
        //创建
        self.lockImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 1, 25, 25)];
        //图片
        _lockImageView.image = [UIImage imageNamed:@"lock1"];
        //添加
        [self.animatedView addSubview:_lockImageView];
        //创建
        self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, 1, 25, 25)];
        //文本
        _placeholderLabel.text = @"密码";
        //字体大小
        _placeholderLabel.font = [UIFont systemFontOfSize:12];
        //文本对齐方式
        _placeholderLabel.textAlignment = NSTextAlignmentCenter;
        //添加
        [self.animatedView addSubview:_placeholderLabel];
    }
    return self;
}

#pragma mark -------changePostion ---------
-(void)changePostion:(CGFloat)pos{
    if (pos > 0) {
        //下移
        
        //边框颜色
        self.inputTextField.layer.borderColor = [UIColor blackColor].CGColor;
        
        //图片
        self.lockImageView.image = [UIImage imageNamed:@"lock1"];
        
        //文本颜色
        self.placeholderLabel.textColor = [UIColor blackColor];
        
    } else {
        //上移
        
        //边框颜色
        self.inputTextField.layer.borderColor = [UIColor greenColor].CGColor;
        
        //图片
        self.lockImageView.image = [UIImage imageNamed:@"lock2"];
        
        //文本颜色
        self.placeholderLabel.textColor = [UIColor greenColor];
        
    }
    
    //动画
    [UIView animateWithDuration:0.3 animations:^{
        
        //移动
        self.animatedView.transform = CGAffineTransformTranslate(self.animatedView.transform, 0, pos);
        
    } completion:^(BOOL finishhed){
        
        //完成之后
        
    }];
}

#pragma mark -------textFieldShouldBeginEditing ---------
//在应该编辑前调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    //第二次密码输入错误的情况下
    if ([_hintLabel.text isEqualToString:@"两次密码不一致"]) {
        _hintLabel.text = @"请重新输入密码确认";
    }
    
    //密码错误的情况下
    if ([_hintLabel.text isEqualToString:@"密码错误"]) {
        _hintLabel.text = @"请重新输入";
    }
    
    //将字体重置为黑色
    _hintLabel.textColor = [UIColor blackColor];
    
    return YES;//默认打开第一响应
}

#pragma mark -------textFieldDidBeginEditing ---------
//在编辑前调用
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    //上滑
    [self changePostion:-28];
    
}

#pragma mark -------textFieldShouldReturn ---------
//Return被点击时要做的事情
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    //收齐键盘
    [_inputTextField resignFirstResponder];
    
    //下滑
    [self changePostion:28];
    
    //设置过密码
    if (self.password.length != 0) {
        //密码正确
        if ([_password isEqualToString:_inputTextField.text]) {
            _hintLabel.text = @"密码正确";
            //延迟两秒告诉主界面要做什么
            [self performSelector:@selector(hidePINLockView) withObject:nil afterDelay:2];
        } else {//密码错误
            _hintLabel.text = @"密码错误";
            _hintLabel.textColor = [UIColor redColor];
        }
    } else {//没有设置过密码
        //没有第一次输入密码
        if (_firstPassword.length == 0) {
            _firstPassword = _inputTextField.text;
            _hintLabel.text = @"请确认密码";
        } else {//有第一次输入密码
            //两次密码相同
            if ([_firstPassword isEqualToString:_inputTextField.text]) {
                _password = _inputTextField.text;
                _hintLabel.text = @"密码设置成功";
                //保存密码
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                //保存密码
                [userDefaults setObject:_firstPassword forKey:@"pin"];
                //延迟两秒告诉主界面要做什么
                [self performSelector:@selector(hidePINLockView) withObject:nil afterDelay:2];
            } else {//两次密码不相同
                _hintLabel.text = @"两次密码不一致";
                _hintLabel.textColor = [UIColor redColor];
            }
        }
    }
    //每一次点击都要清空输入框
    _inputTextField.text = @"";
    
    return YES;
}

#pragma mark -------shouldChangeCharactersInRange ---------
//在编辑中要做的事
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //即将拼接,还没有拼接
    NSString *newString = [_inputTextField.text stringByReplacingCharactersInRange:range withString:string];
    
    //限制最大位数为4
    if (newString.length == 5) {
        //不允许拼接
        return NO;
    }
    //允许拼接
    return  YES;
    
}

#pragma mark -------hidePINLockView ---------
-(void)hidePINLockView{
    self.block(YES);
}

@end

delegate

//
//  ViewController.m
//  PIN密码解锁-自定义(delegate)
//
//  Created by 许磊 on 2019/2/20.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

/**PIN密码解锁---便于加载何种解锁方式*/
@property (nonatomic,strong) PINLockView *pinView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建
    self.pinView = [[PINLockView alloc] initWithFrame:self.view.bounds];
    //设置代理
    _pinView.delegate = self;
    //添加
    [self.view addSubview:_pinView];
    
    
}

- (void)pinLockDidFinished:(BOOL)success{
    if (success == YES) {
        [self.pinView removeFromSuperview];
    } else {
        //根本不会传过来,实际上不需要参数,只需要有触发这个事件就行
    }
}


@end


//
//  PINLockView.h
//  PIN密码解锁-自定义(delegate)
//
//  Created by 许磊 on 2019/2/20.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import <UIKit/UIKit.h>

//希望能被外部访问的数据

//定义一套方法或者说协议或者说代理 通过方法回调数据
@protocol PINLockDelegate <NSObject>
- (void)pinLockDidFinished:(BOOL)success;
@end


@interface PINLockView : UIView

/**定义一个变量delegate*/
@property (nonatomic,assign) id<PINLockDelegate> delegate;

@end


//
//  PINLockView.m
//  PIN密码解锁-自定义(delegate)
//
//  Created by 许磊 on 2019/2/20.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import "PINLockView.h"


@interface PINLockView()<UITextFieldDelegate>

/**定义一个文本提示框*/
@property (nonatomic,strong) UILabel *hintLabel;

/**定义一个文本输入框*/
@property (nonatomic,strong) UITextField *inputTextField;

/**定义一个视图存放锁和提示*/
@property (nonatomic,strong) UIView *animatedView;

/**定义一个显示锁的视图*/
@property (nonatomic,strong) UIImageView *lockImageView;

/**定义一个显示提示的文本的视图*/
@property (nonatomic,strong) UILabel *placeholderLabel;

/**定义存储第一次设置密码的变量*/
@property (nonatomic,strong) NSString *firstPassword;

/**定义存储密码的变量*/
@property (nonatomic,strong) NSString *password;

@end

@implementation PINLockView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self != nil) {
        //创建显示图标
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-90)/2.0, 80, 90, 90)];
        //图片
        imageView.image = [UIImage imageNamed:@"icon"];
        //添加
        [self addSubview:imageView];
        
        //创建文本提示框
        self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, frame.size.width, 40)];
        //文本
        _hintLabel.text = @"欢迎";
        //字体颜色
        _hintLabel.textColor = [UIColor blackColor];
        //字体大小
        _hintLabel.font = [UIFont systemFontOfSize:20];
        //文本对齐方式
        _hintLabel.textAlignment = NSTextAlignmentCenter;
        //添加
        [self addSubview:_hintLabel];
        
        //获取系统自带的NSUserDefaults对象
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        //取数据 密码
        //[userDefaults setObject:@"" forKey:@"pin"];  //清空
        self.password = [userDefaults objectForKey:@"pin"];
        if (_password.length != 0) {
            _hintLabel.text = @"请输入密码";
        } else {
            _hintLabel.text = @"请设置密码";
        }
        
        
        //创建文本输入框
        self.inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 280, frame.size.width-20*2, 45)];
        //样式
        _inputTextField.borderStyle = UITextBorderStyleBezel;
        //边框颜色
        _inputTextField.layer.borderWidth = 1;
        _inputTextField.layer.borderColor = [UIColor blackColor].CGColor;
        //调试
        //_inputTextField.text = @"12345";
        //字体颜色
        _inputTextField.textColor = [UIColor blackColor];
        //字体大小
        _inputTextField.font = [UIFont systemFontOfSize:20];
        //文本对齐方式
        _inputTextField.textAlignment = NSTextAlignmentLeft;
        //设置光标颜色
        _inputTextField.tintColor = [UIColor clearColor];
        //消除按钮
        _inputTextField.clearButtonMode = YES;
        //安全输入
        _inputTextField.secureTextEntry = YES;
        //设置代理
        _inputTextField.delegate = self;
        //左边的视图 看不见
        _inputTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 1)];
        //左边视图的样式
        _inputTextField.leftViewMode = UITextFieldViewModeAlways;
        //添加
        [self addSubview:_inputTextField];
        
        //创建一个视图存放锁和提示
        self.animatedView = [[UIImageView alloc] initWithFrame:CGRectMake(28, 290, 61, 32)];
        //背景颜色
        _animatedView.backgroundColor = [UIColor whiteColor];
        //添加
        [self addSubview:_animatedView];
        //创建
        self.lockImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 1, 25, 25)];
        //图片
        _lockImageView.image = [UIImage imageNamed:@"lock1"];
        //添加
        [self.animatedView addSubview:_lockImageView];
        //创建
        self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, 1, 25, 25)];
        //文本
        _placeholderLabel.text = @"密码";
        //字体大小
        _placeholderLabel.font = [UIFont systemFontOfSize:12];
        //文本对齐方式
        _placeholderLabel.textAlignment = NSTextAlignmentCenter;
        //添加
        [self.animatedView addSubview:_placeholderLabel];
    }
    return self;
}

#pragma mark -------changePostion ---------
-(void)changePostion:(CGFloat)pos{
    if (pos > 0) {
        //下移
        
        //边框颜色
        self.inputTextField.layer.borderColor = [UIColor blackColor].CGColor;
        
        //图片
        self.lockImageView.image = [UIImage imageNamed:@"lock1"];
        
        //文本颜色
        self.placeholderLabel.textColor = [UIColor blackColor];
        
    } else {
        //上移
        
        //边框颜色
        self.inputTextField.layer.borderColor = [UIColor greenColor].CGColor;
        
        //图片
        self.lockImageView.image = [UIImage imageNamed:@"lock2"];
        
        //文本颜色
        self.placeholderLabel.textColor = [UIColor greenColor];
        
    }
    
    //动画
    [UIView animateWithDuration:0.3 animations:^{
        
        //移动
        self.animatedView.transform = CGAffineTransformTranslate(self.animatedView.transform, 0, pos);
        
    } completion:^(BOOL finishhed){
        
        //完成之后
        
    }];
}

#pragma mark -------textFieldShouldBeginEditing ---------
//在应该编辑前调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    //第二次密码输入错误的情况下
    if ([_hintLabel.text isEqualToString:@"两次密码不一致"]) {
        _hintLabel.text = @"请重新输入密码确认";
    }
    
    //密码错误的情况下
    if ([_hintLabel.text isEqualToString:@"密码错误"]) {
        _hintLabel.text = @"请重新输入";
    }
    
    //将字体重置为黑色
    _hintLabel.textColor = [UIColor blackColor];
    
    return YES;//默认打开第一响应
}

#pragma mark -------textFieldDidBeginEditing ---------
//在编辑前调用
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    //上滑
    [self changePostion:-28];
    
}

#pragma mark -------textFieldShouldReturn ---------
//Return被点击时要做的事情
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    //收齐键盘
    [_inputTextField resignFirstResponder];
    
    //下滑
    [self changePostion:28];
    
    //设置过密码
    if (self.password.length != 0) {
        //密码正确
        if ([_password isEqualToString:_inputTextField.text]) {
            _hintLabel.text = @"密码正确";
            //延迟两秒告诉主界面要做什么
            [self performSelector:@selector(hidePINLockView) withObject:nil afterDelay:2];
        } else {//密码错误
            _hintLabel.text = @"密码错误";
            _hintLabel.textColor = [UIColor redColor];
        }
    } else {//没有设置过密码
        //没有第一次输入密码
        if (_firstPassword.length == 0) {
            _firstPassword = _inputTextField.text;
            _hintLabel.text = @"请确认密码";
        } else {//有第一次输入密码
            //两次密码相同
            if ([_firstPassword isEqualToString:_inputTextField.text]) {
                _password = _inputTextField.text;
                _hintLabel.text = @"密码设置成功";
                //保存密码
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                //保存密码
                [userDefaults setObject:_firstPassword forKey:@"pin"];
                //延迟两秒告诉主界面要做什么
                [self performSelector:@selector(hidePINLockView) withObject:nil afterDelay:2];
            } else {//两次密码不相同
                _hintLabel.text = @"两次密码不一致";
                _hintLabel.textColor = [UIColor redColor];
            }
        }
    }
    //每一次点击都要清空输入框
    _inputTextField.text = @"";
    
    return YES;
}

#pragma mark -------shouldChangeCharactersInRange ---------
//在编辑中要做的事
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //即将拼接,还没有拼接
    NSString *newString = [_inputTextField.text stringByReplacingCharactersInRange:range withString:string];
    
    //限制最大位数为4
    if (newString.length == 5) {
        //不允许拼接
        return NO;
    }
    //允许拼接
    return  YES;
    
}

#pragma mark -------hidePINLockView ---------
-(void)hidePINLockView{
    //回调数据
    //是否实现了我们的方法
    if ([self.delegate respondsToSelector:@selector(pinLockDidFinished:)]) {
        [self.delegate pinLockDidFinished:YES];
    }
}

@end

运行结果

运行结果

开发流程

1.创建一个类 PINLockView 继承于UIView
2.通过delegate或者block实现数据的回调
3.布局PINLockView

  • 创建logo图
  • 创建label
  • 创建textfield并实现代理方法
  • 创建一个背景视图 用于存放图片视图和文本label,可以同时操作两个控件 ,便于
    移动

4.密码 使 NSUserDefaults

附一:遇到的问题

  • 1.小型数据长时间保存---NSUserDefaults

和字典的用法相同,这个过程实际上是操纵plist文件实现的

//1.获取系统自带的NSUserDefaults对象 单例对象 确保整个程序中操作的都是这个NSUserDefaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//2.存数据
[userDefaults setObject:@"123" forKey:@"pin"]; 
[userDefaults synchronize];//同步
//3.取数据
NSString *str = [userDefaults objectForKey:@"pin"];
  • 2.单例操作---扩展

  • 3.归档数据的几种方式:
    大量数据:coreData,sqlite3
    音频,视频:file
    小型数据:NSUserDefaults

附二:个人改进点

  • 可以考虑多种解锁方式

相关文章

网友评论

    本文标题:密码解锁---改进(block,deldgate回调数据)

    本文链接:https://www.haomeiwen.com/subject/psoaeqtx.html