前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 输入支付密码demo

iOS 输入支付密码demo

作者头像
honey缘木鱼
发布2019-07-01 11:18:02
2K0
发布2019-07-01 11:18:02
举报
文章被收录于专栏:娱乐心理测试娱乐心理测试

一.效果图:

实现思路: 1.输入显示试图和系统键盘的出现和隐藏。 2.用UILabel和UITextField实现输入框。

二.关键代码:

代码语言:javascript
复制
//试图的出现和隐藏方法
- (void)showPayPopView
{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:AnimationTimeInterval animations:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.superView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.3];
    } completion:nil];
}

- (void)hidePayPopView
{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:AnimationTimeInterval animations:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.superView.alpha = 0.0;
        strongSelf.frame = CGRectMake(strongSelf.frame.origin.x, kScreenWidth, strongSelf.frame.size.width, strongSelf.frame.size.height);
    } completion:^(BOOL finished) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.superView removeFromSuperview];
        strongSelf.superView = nil;
    }];
}

//输入密码或删除时

代码语言:javascript
复制
- (void)updateLabelBoxWithText:(NSString *)text
{
    //输入时
    if (text.length > self.currentText.length) {
        for (int i = 0; i < PasswordBoxNumber; i++)
        {
            UILabel *label = self.labelBoxArray[i];
            if (i < text.length - 1)
            {
                //特殊字符不居中显示,设置文本向下偏移
                NSAttributedString * att1 = [[NSAttributedString alloc] initWithString:@"*" attributes:@{NSBaselineOffsetAttributeName:@(-3)}];
                label.attributedText = att1;
            }
            else if (i == text.length - 1)
            {
                label.text = [text substringWithRange:NSMakeRange(i, 1)];
                [self animationShowTextInLabel: label];
            }
            else
            {
                label.text = @"";
            }
        }
    }
    //删除时
    else
    {
        for (int i = 0; i < PasswordBoxNumber; i++)
        {
            UILabel *label = self.labelBoxArray[i];
            if (i < text.length)
            {
                //特殊字符不居中显示,设置文本向下偏移
                NSAttributedString * att1 = [[NSAttributedString alloc] initWithString:@"*" attributes:@{NSBaselineOffsetAttributeName:@(-3)}];
                label.attributedText = att1;
            }
            else
            {
                label.text = @"";
            }
        }
    }
    self.textField.text = text;
    self.currentText = text;
}

//block回调拿到输入的密码字符

代码语言:javascript
复制
- (void)textDidChanged:(UITextField *)textField
{
    if (textField.text.length > PasswordBoxNumber)
    {
        textField.text = [textField.text substringToIndex:PasswordBoxNumber];
    }
    
    [self updateLabelBoxWithText:textField.text];
    if (textField.text.length == PasswordBoxNumber)
    {
        if (self.completionBlock)
        {
            self.completionBlock(self.textField.text);
        }
    }
}

demo下载地址:https://github.com/dt8888/PursePossword

三.demo 的使用

1.初始化试图

代码语言:javascript
复制
self.payPopupView = [[PursePopupView alloc]initWithFrame:CGRectMake(0, XX_6(310), kScreenWidth, kScreenHeight)];
 self.payPopupView.delegate = self;
 [self.payPopupView showPayPopView];

2.实现代理方法拿到输入密码字符串

代码语言:javascript
复制
- (void)didPasswordInputFinished:(NSString *)password
{
    if ([password isEqualToString:@"123456"])
    {
        NSLog(@"输入的密码正确");
    }
    else
    {
        NSLog(@"输入错误:%@",password);
        [self.payPopupView didInputPayPasswordError];
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.效果图:
  • 二.关键代码:
  • 三.demo 的使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档