iOS-验证码倒计时60秒
一. 要求
1.点击获取验证码按钮,60秒倒计时,按钮变成不可点击状态,按钮文字变成倒计时的秒数.
2.当倒计时为0的时候,释放掉定时器NSTimer,按钮变成可以点击状态,按钮文字变成"获取验证码".
二.
// ViewController.m
// Demo-验证码
//
// Created by quhaola on 16/4/11.
// Copyright © 2016年 MC. All rights reserved.
//
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
{
NSTimer *_timer; //定时器
NSInteger _second; //倒计时的时间
}
@property (nonatomic, strong) UITextField * textField;
@property (nonatomic, strong) UIButton * authCodeButton;
@end
@implementation ViewController
#pragma mark - 生命周期
- (void)viewDidLoad
{
[super viewDidLoad];
//创建验证码按钮
[self addAuthCodeButton];
}
#pragma mark - 实现方法
- (void)addAuthCodeButton
{
//输入框
[self.view addSubview:self.textField];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).with.offset(10);
make.top.equalTo(self.view.mas_top).with.offset(100);
make.height.mas_equalTo(40);
make.width.mas_equalTo(200);
}];
//获取验证码按钮
[self.view addSubview:self.authCodeButton];
[self.authCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.textField.mas_right).with.offset(20);
make.right.equalTo(self.view.mas_right).with.offset(-20);
make.top.equalTo(self.view.mas_top).with.offset(100);
make.height.mas_equalTo(40);
}];
}
#pragma mark - 点击事件
//获取验证码按钮的点击事件
- (void)authCodeButtonClicked
{
// 按钮点击之后的操作 ---> 按钮不可点击,按钮背景颜色改变
self.authCodeButton.enabled = NO;
self.authCodeButton.backgroundColor = [UIColor grayColor];
// 设置倒计时的总时间
_second = 10;
// 创建计时器
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
// (可以不考虑) 把清空textField数据
self.textField.text = nil;
}
#pragma mark 定时器调用的方法
- (void)changeTime
{
//每一秒调用一次改方法, 每次调用,_second 减一.
_second --;
//修改倒计时标签文字 -> 把按钮文字改成倒计时的时间
[self.authCodeButton setTitle:[NSString stringWithFormat:@"%@ s",@(_second)] forState:UIControlStateNormal];
//如果时间到了 0 秒, 把定时器取消掉
if (_second == -1)
{
//释放定时器
[_timer invalidate];
//把定时器设置成空.不然不起作用.
_timer = nil;
//把修改的验证码按钮调整为初始状态
self.authCodeButton.enabled = YES;
[self.authCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
self.authCodeButton.backgroundColor = [UIColor orangeColor];
self.textField.text = @"这里是你请求的验证码";
}
}
#pragma mark - 验证码按钮
- (UITextField *)textField
{
if (!_textField)
{
self.textField = [[UITextField alloc] init];
self.textField.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
}
return _textField;
}
- (UIButton *)authCodeButton
{
if (!_authCodeButton)
{
self.authCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.authCodeButton.backgroundColor = [UIColor orangeColor];
[self.authCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
[self.authCodeButton addTarget:self action:@selector(authCodeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
return _authCodeButton;
}
@end
iOS-验证码倒计时60秒的更多相关文章
- js 验证码 倒计时60秒
js 验证码 倒计时60秒 <input type="button" id="btn" value="免费获取验证码" /> & ...
- vue实现验证码倒计时60秒的具体代码
vue实现验证码倒计时60秒的具体代码 <span v-show="show" @click="getCode">获取验证码</span> ...
- jQuery实现发送验证码倒计时60秒
前端HMTL: <div class="form_box"> <div class="line mb40"> <div class ...
- javaScript 验证码 倒计时60秒
<input type="button" id="btn" value="免费获取验证码" /> <script type ...
- Android 实现简单 倒计时60秒,一次1秒
倒计时功能如上图所示,其实就几行代码即可实现效果啦!!! /** 倒计时60秒,一次1秒 */ CountDownTimer timer = new CountDownTimer(60*1000, 1 ...
- js实现倒计时60秒的简单代码
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- js 简单实现获取短信按钮倒计时60秒
<!DOCTYPE html><html lang="en"><head> <meta http-equiv="Content- ...
- Jquery插件实现点击获取验证码后60秒内禁止重新获取
通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能 先到官网(http://plugins.jquery.com/cookie/ )下载coo ...
- jQuery实现发送短信验证码后60秒倒计时
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
随机推荐
- AP6181 正基 WIFI 模块
a. Module size: 12*12mm (pin to pin compatible) Package: Stamp type 44pins AP6181: WiFiAP6210: WiFi/ ...
- PIC24FJ64GB002 with bluetooth USB dongle
PIC24FJ64GB002 with bluetooth USB dongle I will explain my project (how to control a bluetooth USB d ...
- [1].jekyll扫盲
一.jekyll是什么? jekyll是一款免费的blog生成工具,将纯文本(plain text)转换为静态网站或博客. Jekyll是一个使用Ruby编写的静态站点生成工具,使用Liquid模板渲 ...
- 那些在学习iOS开发前就应该知道的事(part 1)
英文原文:Things I wish I had known before starting iOS development—Part 1 http://www.cocoachina.com/ios/ ...
- nginx内置全局变量及含义
名称 版本 说明(变量列表来源于文件 ngx_http_variables ) $args 1.0.8 请求中的参数; $binary_remo ...
- [转]揭秘webdriver实现原理
转自:http://www.cnblogs.com/timsheng/archive/2012/06/12/2546957.html 通过研究selenium-webdriver的源码,笔者发现其实w ...
- Hello Kraken.js!
前言 kraken.js 由paypal 公司开源的一个用于快速开发基于Express.js框架应用的快速开发工具, 因为kraken 并没有在Express.js基础上更改多少东西,只是在原来的 ...
- Apache Error: Invalid command ‘Allow’, perhaps misspelled or defined by a module not included in the server configuration
在一个Window Server 2008R2系统上使用Apache架设了一个PHP的网站项目 在配置Apache的过程中出现了以下问题 根据上面的提示说是没有相应的权限,那就在虚拟主机里进行了配 ...
- nginx location 的配置
一.基本语法:location [=|~|~*|^~] /uri/ { … } 二.分类: 1.基本location:以“ = ”或“ ^~ ”为前缀或者没有任何前缀的 /uri/ 2.正则locat ...
- 解决Win7旗舰版开机后无线网络识别非常慢的问题
最近电脑开机后WIFI识别和连接非常慢,不知何故.查看百度安全卫士的优化记录,发现其禁用了 Network List Service,将该服务设为自动启动,重启服务后,问题解决.PS:如此优化太可恶!