#define kDotSize CGSizeMake(10, 10) //密码点的大小#define kDotCount 6//密码个数#define k_Field_Height 45 //每一个输入框的高度
@interface ViewController6 (){
UILabel* infoLab ;//提示信息
UIView* scrView;
UITextField* textField1;//输入框
UIView* topView;//顶部试图
double duration;
UIView *animateView;//加载动画
}
@property (nonatomic, strong) NSMutableArray *dotArray; //用于存放黑色的点点
@property (nonatomic, strong) FLAnimatedImageView *imageView1;
@end
@implementation ViewController6
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - 监听方法
/**
* 键盘的frame发生改变时调用(显示、隐藏等)
*/
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
// if (self.picking) return;
/**
notification.userInfo = @{
// 键盘弹出\隐藏后的frame
UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 352}, {320, 216}},
// 键盘弹出\隐藏所耗费的时间
UIKeyboardAnimationDurationUserInfoKey = 0.25,
// 键盘弹出\隐藏动画的执行节奏(先快后慢,匀速)
UIKeyboardAnimationCurveUserInfoKey = 7
}
*/
NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 键盘的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 执行动画
[UIView animateWithDuration:duration animations:^{
// _blackBgView.alpha = 0.410;
// 工具条的Y值 == 键盘的Y值 - 工具条的高度
if (keyboardF.origin.y > self.view.height) { // 键盘的Y值已经远远超过了控制器view的高度
scrView.hidden = YES;
} else {
scrView.top = keyboardF.origin.y - scrView.height;
scrView.hidden = NO;
topView.height = self.view.height- scrView.height-keyboardF.size.height;
}
} completion:^(BOOL finished) {
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"交易信息";
[self initInterface];
[self initPwdTextField];
[self initImage];
}
- (void)initInterface{
self.view.backgroundColor = [UIColor whiteColor];
UIButton* but = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:but];
but.frame = CGRectMake(100, 100, 50, 50);
but.backgroundColor = [UIColor redColor];
[but addTarget:self action:@selector(didBut) forControlEvents:UIControlEventTouchUpInside];
scrView = [[UIView alloc]init];
[self.view addSubview:scrView];
scrView.backgroundColor = [UIColor colorWithHex:0xFFFFFF];
scrView.hidden = YES;
scrView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height/3.0, [UIScreen mainScreen].bounds.size.width, 193);
topView = [[UIView alloc]init];
[self.view addSubview:topView];
topView.backgroundColor = [[UIColor colorWithHex:0xC5C5C5]colorWithAlphaComponent:0.5];
topView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0);
topView.hidden = YES;
UITapGestureRecognizer* tapGesRec = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTopView)];
[topView addGestureRecognizer:tapGesRec];
infoLab = [[UILabel alloc]init];
[scrView addSubview:infoLab];
infoLab.frame = CGRectMake(50, 5, [UIScreen mainScreen].bounds.size.width-100, 20);
infoLab.textAlignment = NSTextAlignmentCenter;
infoLab.textColor = [UIColor colorWithHex:0x333333];
infoLab.text = @"输入6位交易密码";
infoLab.font = [UIFont systemFontOfSize:15.0];
textField1 = [[UITextField alloc]init];
[scrView addSubview:textField1];
textField1.delegate = self;
textField1.secureTextEntry =YES;
textField1.backgroundColor = [UIColor whiteColor];
textField1.frame = CGRectMake(53, infoLab.bottom+8, scrView.width-106, k_Field_Height);
textField1.keyboardType = UIKeyboardTypeNumberPad;
[textField1 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//输入的文字颜色为白色
textField1.textColor = [UIColor whiteColor];
//输入框光标的颜色为白色
textField1.tintColor = [UIColor whiteColor];
textField1.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField1.layer.borderColor = [[UIColor lightGrayColor] CGColor];
textField1.layer.borderWidth = 1;
textField1.layer.cornerRadius = 5;
textField1.secureTextEntry = YES;
textField1.font = [UIFont systemFontOfSize:3];
UIButton* butForget = [UIButton buttonWithType:UIButtonTypeCustom];
[scrView addSubview:butForget];
butForget.frame = CGRectMake(50, textField1.bottom+25, [UIScreen mainScreen].bounds.size.width-100, 20);
[butForget setTitle:@"忘记密码" forState:UIControlStateNormal];
[butForget setTitleColor:[UIColor colorWithHex:0x67B8F3] forState:UIControlStateNormal];
[butForget addTarget:self action:@selector(didButForget) forControlEvents:UIControlEventTouchUpInside];
butForget.titleLabel.font = [UIFont systemFontOfSize:15.0];
butForget.contentVerticalAlignment = NSTextAlignmentCenter;
animateView = [[UIView alloc]init];
[self.view addSubview:animateView];
animateView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
animateView.hidden = YES;
animateView.backgroundColor = [[UIColor colorWithHex:0xCCCCCC]colorWithAlphaComponent:0.5];
UIView* loadAnimateView = [[UIView alloc]init];
[animateView addSubview:loadAnimateView];
loadAnimateView.backgroundColor = [UIColor whiteColor];
loadAnimateView.frame = CGRectMake(0, 222, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height-222 );
self.imageView1 = [[FLAnimatedImageView alloc]init];
NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"123" withExtension:@"gif"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
FLAnimatedImage *animatedImage1 = [FLAnimatedImage animatedImageWithGIFData:data1];
self.imageView1.animatedImage = animatedImage1;
self.imageView1.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-100)/2.0, 158, 100, 100);
[loadAnimateView addSubview:self.imageView1];
UILabel* animateLab = [[UILabel alloc]init];
[loadAnimateView addSubview:animateLab];
animateLab.frame = CGRectMake(30, self.imageView1.bottom, [UIScreen mainScreen].bounds.size.width-60, 20);
animateLab.textAlignment = NSTextAlignmentCenter;
animateLab.textColor = [UIColor colorWithHex:0x7D7D7D];
animateLab.font = [UIFont systemFontOfSize:15];
animateLab.text = @"提交成功,请稍候...";
}
- (void)didTopView{
[textField1 resignFirstResponder];
topView.hidden = YES;
scrView.hidden = YES;
}
- (void)didButForget{
NSLog(@"忘记密码");
infoLab.text = [NSString stringWithFormat:@"密码错误,还剩%@次机会",@(2)];
infoLab.textColor = [UIColor colorWithHex:0xF61B1C];
}
- (void)didBut{
NSLog(@"是否输入密码");
[textField1 becomeFirstResponder];
scrView.hidden = NO;
topView.hidden = NO;
}
#pragma mark----禁止复制粘贴
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
}
- (void)initPwdTextField
{
//每个密码输入框的宽度
CGFloat width = ([UIScreen mainScreen].bounds.size.width - 106) / kDotCount;
//生成分割线
for (int i = 0; i < kDotCount - 1; i++) {
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0 + (i + 1) * width, 0, 1, k_Field_Height)];
lineView.backgroundColor = [UIColor colorWithHex:0xCCCCCC];
[textField1 addSubview:lineView];
}
self.dotArray = [[NSMutableArray alloc] init];
//生成中间的点
for (int i = 0; i < kDotCount; i++) {
UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(0 + (width - kDotCount) / 2 + i * width, 0+ (k_Field_Height - kDotSize.height) / 2, kDotSize.width, kDotSize.height)];
dotView.backgroundColor = [UIColor colorWithHex:0x4D4D4D];
dotView.layer.cornerRadius = kDotSize.width / 2.0f;
dotView.clipsToBounds = YES;
dotView.hidden = YES; //先隐藏
[textField1 addSubview:dotView];
//把创建的黑色点加入到数组中
[self.dotArray addObject:dotView];
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"变化%@", string);
if([string isEqualToString:@"\n"]) {
//按回车关闭键盘
[textField resignFirstResponder];
return NO;
} else if(string.length == 0) {
//判断是不是删除键
return YES;
}
else if(textField.text.length >= kDotCount) {
//输入的字符个数大于6,则无法继续输入,返回NO表示禁止输入
NSLog(@"输入的字符个数大于6,忽略输入");
return NO;
} else {
return YES;
}
}
/**
* 清除密码
*/
- (void)clearUpPassword
{
textField1.text = @"";
[self textFieldDidChange:textField1];
}
/**
* 重置显示的点
*/
- (void)textFieldDidChange:(UITextField *)textField
{
NSLog(@"%@", textField.text);
for (UIView *dotView in self.dotArray) {
dotView.hidden = YES;
}
for (int i = 0; i < textField.text.length; i++) {
((UIView *)[self.dotArray objectAtIndex:i]).hidden = NO;
}
if (textField.text.length == kDotCount) {
NSLog(@"输入完毕");
NSLog(@"%@",textField1.text);
[textField1 resignFirstResponder];
topView.hidden = YES;
scrView.hidden = YES;
animateView.hidden = NO;
}
}
- (void)initImage{
// UIImage *animatedImage = [UIImage animatedImageWithImages:images duration:duration];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}