密码解锁

作者: 小石头呢 | 来源:发表于2019-02-27 08:44 被阅读10次

项目源代码
链接:
https://pan.baidu.com/s/1wjKrN2bxFpatbx7dqbnamA 密码:f350

主要代码:

//
//  ViewController.h
//  密码解锁
//
//  Created by 许磊 on 2019/2/18.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//可以被外部访问到的

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



@end




//
//  ViewController.m
//  密码解锁
//
//  Created by 许磊 on 2019/2/18.
//  Copyright © 2019年 xulei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

//不想被外部访问的属性

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

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

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

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

/**定义一个BOOL类型变量判断是否设置过密码*/
@property (nonatomic,assign) BOOL passworded;

/**默认初始密码*/
@property (nonatomic,strong) NSString *firstPassword;

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**密码状态*/
    [self passwordUIInit];
    
    /**图片图标布局显示*/
    [self iconUIInit];
    
    /**文本提示框布局*/
    [self hintLabelUIInit];
    
    /**文本输入框布局*/
    [self inputTextFieldUIInit];
    
    /**动画视图布局*/
    [self animatedViewUIInit];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if (_passworded == YES) {
        //设置过密码
        _hintLabel.text = @"请输入密码";
    } else {
        //没有设置过密码
        _hintLabel.text = @"请设置密码";
    }
}

#pragma mark -------passwordUIInit ---------
-(void)passwordUIInit{
    //未设置过密码
    _passworded = NO;

    //设置过密码
    //_passworded = YES;
   // _password = @"1234";

}

#pragma mark -------iconUIInit ---------
-(void)iconUIInit{
    //创建
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-90)/2, 80, 90, 90)];
    //图片
    imageView.image = [UIImage imageNamed:@"icon"];
    //图片填充方式-按图片宽高比缩放,不变形
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //添加
    [self.view addSubview:imageView];
}

#pragma mark -------hintLabel ---------
-(void)hintLabelUIInit{
    //创建
    self.hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 40)];
    //文本
    _hintLabel.text = @"欢迎";
    //字体颜色
    _hintLabel.textColor = [UIColor blackColor];
    //字体大小
    _hintLabel.font = [UIFont systemFontOfSize:20];
    //文本对齐方式
    _hintLabel.textAlignment = NSTextAlignmentCenter;
    //添加
    [self.view addSubview:_hintLabel];
}

#pragma mark -------inputTextField ---------
-(void)inputTextFieldUIInit{
    //创建
    self.inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 280, self.view.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.view addSubview:_inputTextField];
}

#pragma mark -------animatedView ---------
-(void)animatedViewUIInit{
    //创建
    self.animatedView = [[UIImageView alloc] initWithFrame:CGRectMake(28, 290, 61, 32)];
    //背景颜色
    _animatedView.backgroundColor = [UIColor whiteColor];
    //添加
    [self.view 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];
}

#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 (_passworded == YES) {
        //密码正确
        if ([_password isEqualToString:_inputTextField.text]) {
            _hintLabel.text = @"密码正确";
        } 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 = @"密码设置成功";
            } 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;
    
}

@end

运行结果:

没有设置初始密码 设置过初始密码

密码登录界面开发流程

1.UITextField UILabel
2.界面布局

  • 1.创建文本UILabel
  • 2.UITextField
  • 3.程序运行起来弹出一个键盘 等待输入 让textField成为第一响应者 becomeFirstResponder
  • 4.配置label提示的内容 通过一个变量来记录状态 NO:没有设置 YES: 设置过
  • 5.设置密码
    a.设置一个变量于保存用户第一次输入的密码
    b.响应键盘的return按钮的事件
    UITextFieldDelegate- >TextFieldShouldReturn
    c.判断进入TextFieldShouldReturn的状态 第一次 还是第二次输入密码
    firstInputPwdString是否有值
    1. 较密码是否相同 isEqualToString
      错误情况颜色变红 textFieldDidBeginEditing 变黑
    1. 限制输入6位密码

附一:遇到的问题

如何使用UITextField的delegate需要实现的一些方法

关系图

附二:个人改进点

自定义一个类用于管理PIN密码输入

  • 1.封装操作
  • 2.可移植 下次用着方便
  • 3.减轻界面的代码压力 冗余

相关文章

网友评论

    本文标题:密码解锁

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