密码解锁---改进(使用消息中心传递数据)
2019-03-01 本文已影响5人
小石头呢
源代码
链接:
https://pan.baidu.com/s/1dnfyL9BQI1Xb5w81R0JJYw 密码:195q
主要代码:
//
// PINLockView.m
// PIN密码解锁-自定义(消息中心)
//
// Created by 许磊 on 2019/2/21.
// 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{
//发送消息
//获得应用中心的消息中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//字典
NSDictionary *dic = @{@"name":@"jack",@"pwd":@"123"};
//发送消息 消息名以及内容
[center postNotificationName:@"testNotificationName" object:dic];
}
@end
//
// ViewController.m
// PIN密码解锁-自定义(消息中心)
//
// Created by 许磊 on 2019/2/21.
// Copyright © 2019年 xulei. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
/**定义一个PINLockView*/
@property (nonatomic,strong)PINLockView *pinView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建
self.pinView = [[PINLockView alloc] initWithFrame:self.view.bounds];
//添加
[self.view addSubview:_pinView];
//获得应用程序的消息中心
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册消息 监听一个消息
[center addObserver:self selector:@selector(test:) name:@"testNotificationName" object:nil];
}
- (void)test:(NSNotification *)noti{
//消息名称答应
NSLog(@"%@",noti.name);
//获取消息的内容 并打印
NSString *uName = [noti.object objectForKey:@"name"];
NSString *uPwd = [noti.object objectForKey:@"pwd"];
NSLog(@"%@ %@",uName,uPwd);
//要做的事
[self.pinView removeFromSuperview];
}
@end
运行结果
![](https://img.haomeiwen.com/i13294749/6d6b9d841996ae2f.gif)
附一:消息中心传递数据
1.获得应用程序的消息中心对象
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
2.注册消息 监听一个消息
[center addObserver:self selector:@selector(test:) name:@"testNotificationName"
object:nil];
- 1.observer: 谁来监听这个消息
- 2.selector :消息触发了要去做什么事情
- 3.name:监听的消息的名称
- 4.object::注册消息的时候 是否需要向消息中心传递数据
3.发送消息
[center postNotificationName:@"testNotificationName" object:nil];
NSNotification封装消息的内容
- name 消息的名称
- object 消息的内容
4.事件
如果不需要参数
(void)test{};
如果需要参数
(void) test: (NSNotification *)noti{};
附二:无法打开的问题
Xcode打开工程出现:Failed to load project at ‘’xxx”,incompatible project version
1.错误原因:
由于工程是低版本的Xcode建立的,在使用高版本的Xcode打开时会出现编译不了工程。
2.解决方法:
鼠标右击.xcodeproj文件 —》显示包内容 —》打开project.pbxproj文件,比较以前的版本号进行修改(比如:把objecVersion=50修改objecVersion=48即可打开工程)
Xcode工程出现:A build only device cannot be used to run this target.
1.错误原因
造成这个错误的原因是,你选择的版本号,在虚拟机中没有,
所以我们要重新选择版本号
2.解决方法