zoukankan      html  css  js  c++  java
  • 简单的“获取验证码”的按钮功能实现

    最近做项目遇到一个小问题,获取码证码,通常这种界面直接用xib来布局这个注册页面,以往用的是纯代码,然后发现了一个小bug,以前用纯代码写的获取验证码这个功能正常的,但是现在用xib拖拽的按钮,虽然可以实现,但是文字会闪烁,这到底是为什么纯代码没有问题,xib就出现这个问题呢?我也不知道,其实这里第一次写这个博客,写得有点烂,先上代码

     

    #import "ViewController.h"

    @interface ViewController ()

     

    /* 时间 */

    @property (nonatomic, assign) int timeCount;

    /* 定时器 */

    @property (nonatomic, strong) NSTimer *timer;

    /* 按钮 */

    @property (nonatomic, weak) UIButton *messageButton;

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        

        //创建按钮

        UIButton *btn = [[UIButton alloc] init];

        [btn addTarget:self action:@selector(sendMessageClick) forControlEvents:UIControlEventTouchUpInside];

        [btn setTitle:@"获取码证码" forState:UIControlStateNormal];

        btn.frame = CGRectMake(100, 100, 200, 40);

        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        btn.backgroundColor = [UIColor redColor];

        [self.view addSubview:btn];

        self.messageButton = btn;

    }

     

    //侦听

    - (void)sendMessageClick

    {

        self.messageButton.backgroundColor = [UIColor grayColor];

        self.messageButton.userInteractionEnabled = NO;

        //开始倒计时

        [self beginCalculationTime];

    }

     

    //开始倒计时

    - (void)beginCalculationTime

    {

        self.timeCount = 10;

        [self.messageButton setTitle:@"重新获取(60)" forState:UIControlStateNormal];

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];

        [self.timer fire];

    }

     

    //timer

    - (void)refreshTime

    {

        self.label.text = [NSString stringWithFormat:@"重新获取(%ld)",(long)self.timeCount];

        self.timeCount -= 1;

        [self.messageButton setTitle:[NSString stringWithFormat:@"重新获取(%ld)",(long)self.timeCount] forState:UIControlStateNormal];

        if (self.timeCount == 0) {

            [self.messageButton setTitle:@"重新获取" forState:UIControlStateNormal];

            [self.timer invalidate];

            self.messageButton.userInteractionEnabled = YES;

            self.messageButton.backgroundColor = [UIColor redColor];

        }

    }

     

    @end

  • 相关阅读:
    烂泥:学习ubuntu之快速搭建LNMP环境
    烂泥:学习ubuntu远程桌面(二):远程桌面会话管理
    烂泥:学习ubuntu远程桌面(一):配置远程桌面
    烂泥:学习ssh之ssh密钥随身携带
    烂泥:学习ssh之ssh无密码登陆
    JS 获取浏览器窗口大小
    connect() failed (111: Connection refused) while connecting to upstream的解决
    css加载没效果,查看网络显示类型为 text/plain 的解决方法
    empty和isset的区别
    SQLite3命令操作大全
  • 原文地址:https://www.cnblogs.com/ljj-Andrew-519/p/7119039.html
Copyright © 2011-2022 走看看