验证码倒计时按钮的应用是非常普遍的,该Blog就和你一起来写一个IDCountDownButton来实现验证码倒计时的效果。你可以想使用普通的UIButton类型按钮一样,只需要设置其倒计时时长(若未设置,默认为60秒),就可以轻松的实现点击countDownButton开始倒计时,倒计时结束方可重新点击。

一、实现效果
如图

二、实现思路
1、自定义一个IDCountDownButton,重写 beginTrackingWithTouch:withEvent: 拦截button的点击事件,根据是否正在倒计时决定是否响应并传递button的点击事件(若倒计时正在进行中,再次点击不会重新开始倒计时)
2、是用NSTimer定时器,定时改变IDCountDownButton的title
3、若倒计时结束,取消定时器并回复倒计时时长(使IDCountDownButton具备再次开始倒计时的能力)
4、在IDCountDownButton销毁时,同样取消定时器
三、实现步骤
1、添加相关的属性
公有属性(public)

1
2
3
4
@interface IDCountDownButton : UIButton
/** 验证码倒计时的时长 */
@property (nonatomic, assign) NSInteger durationOfCountDown;
@end

私有属性

1
2
3
4
5
6
7
8
@interface IDCountDownButton ()
/** 保存倒计时按钮的非倒计时状态的title */
@property (nonatomic, copy) NSString *originalTitle;
/** 保存倒计时的时长 */
@property (nonatomic, assign) NSInteger tempDurationOfCountDown;
/** 定时器对象 */
@property (nonatomic, strong) NSTimer *countDownTimer;
@end

2、重写setter
title属性的setter
1)、私有属性originalTitle用来暂存开始计时前button的标题,即用户设置的button的标题,通常是“获取验证码”
2)、需要屏蔽计时过程中,title更新时改变originalTitle的值

1
2
3
4
5
6
7
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
 [super setTitle:title forState:state];
 // 倒计时过程中title的改变不更新originalTitle
 if (self.tempDurationOfCountDown == self.durationOfCountDown) {
  self.originalTitle = title;
 }
}

durationOfCountDown属性的setter
1)、设置tempDurationOfCountDown的值
2)、tempDurationOfCountDown的作用:倒计时;与durationOfCountDown配合判断当前IDCountDownButton是否具备重新开始倒计时的能力

1
2
3
4
- (void)setDurationOfCountDown:(NSInteger)durationOfCountDown {
 _durationOfCountDown = durationOfCountDown;
 self.tempDurationOfCountDown = _durationOfCountDown;
}

初始化
1)、设置倒计时的默认时长为60妙
2)、设置IDCountDownButton默认的title为“获取验证码”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (instancetype)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
  // 设置默认的倒计时时长为60秒
  self.durationOfCountDown = 60;
  // 设置button的默认标题为“获取验证码”
  [self setTitle:@"获取验证码" forState:UIControlStateNormal];
 }
 return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
 if (self = [super initWithCoder:aDecoder]) {
  // 设置默认的倒计时时长为60秒
  self.durationOfCountDown = 60;
  // 设置button的默认标题为“获取验证码”
  [self setTitle:@"获取验证码" forState:UIControlStateNormal];
 }
 return self;
}

拦截IDCountDownButton的点击事件,判断是否开始倒计时
1)、若tempDurationOfCountDown等于durationOfCountDown,说明未开始倒计时,响应并传递IDCountDownButton的点击事件;否则,不响应且不传递。

1
2
3
4
5
6
7
8
9
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
 // 若正在倒计时,不响应点击事件
 if (self.tempDurationOfCountDown != self.durationOfCountDown) {
  return NO;
 }
 // 若未开始倒计时,响应并传递点击事件,开始倒计时
 [self startCountDown];
 return [super beginTrackingWithTouch:touch withEvent:event];
}

倒计时
1)、创建定时器,开始倒计时

1
2
3
4
5
6
- (void)startCountDown {
 // 创建定时器
 self.countDownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateIDCountDownButtonTitle) userInfo:nil repeats:YES];
 // 将定时器添加到当前的RunLoop中(自动开启定时器)
 [[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes];
}

2)、更新IDCountDownButton的title为倒计时剩余的时间

1
2
3
4
5
6
7
8
9
10
11
12
- (void)updateIDCountDownButtonTitle {
 if (self.tempDurationOfCountDown == 0) {
  // 设置IDCountDownButton的title为开始倒计时前的title
  [self setTitle:self.originalTitle forState:UIControlStateNormal];
  // 恢复IDCountDownButton开始倒计时的能力
  self.tempDurationOfCountDown = self.durationOfCountDown;
  [self.countDownTimer invalidate];
 } else {
  // 设置IDCountDownButton的title为当前倒计时剩余的时间
  [self setTitle:[NSString stringWithFormat:@"%zd秒", self.tempDurationOfCountDown--] forState:UIControlStateNormal];
 }
}

3)、移除定时器

1
2
3
- (void)dealloc {
 [self.countDownTimer invalidate];
}

使用示例
1)、添加vertificationCodeIDCountDownButton属性

1
2
3
4
@interface ViewController ()
/** 验证码倒计时的button */
@property (nonatomic, strong) IDCountDownButton *vertificationCodeIDCountDownButton;
@end

2)、创建vertificationCodeIDCountDownButton并进行相关设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)viewDidLoad {
 [super viewDidLoad];
 // 创建vertificationCodeIDCountDownButton
 self.vertificationCodeIDCountDownButton = [[IDCountDownButton alloc] initWithFrame:CGRectMake(160, 204, 120, 44)];
 // 添加点击事件
 [self.vertificationCodeIDCountDownButton addTarget:self action:@selector(vertificationCodeIDCountDownButtonClick:) forControlEvents:UIControlEventTouchUpInside];
 // 设置标题相关属性
 [self.vertificationCodeIDCountDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 [self.vertificationCodeIDCountDownButton setTitle:@"获取验证码" forState:UIControlStateNormal];
 // 设置背景图片
 [self.vertificationCodeIDCountDownButton setBackgroundImage:[UIImage imageNamed:@"redButton"] forState:UIControlStateNormal];
 // 设置倒计时时长
 self.vertificationCodeIDCountDownButton.durationOfCountDown = 10;
 // 将vertificationCodeIDCountDownButton添加的控制器的view中
 [self.view addSubview:self.vertificationCodeIDCountDownButton];
}

3)、实现点击事件触发的操作

1
2
3
- (void)vertificationCodeIDCountDownButtonClick:(UIButton *)button {
 // TODO:调用服务器接口,获取验证码
}

四、关于AppIcon
添加AppIcon时需要遵循以下规则
1)、命名,以Icon开头(首字母大写),跟上@2x/@3x,如图:

2)、尺寸,必须按要求设置尺寸,如图

3)、图中所示的60pt对应的图片尺寸是
2x:120px X 120px
3x:180px X 180px

以上就是本文的全部内容,希望对大家的学习有所帮助。

iOS-实现验证码倒计时功能(1)的更多相关文章

  1. js之验证码倒计时功能

    <!DOCTYPE html> <html > <head> <meta http-equiv="Content-Type" conten ...

  2. jq 实现发送验证码倒计时功能

    var util = { wait:60, hsTime: function (that) { _this = this; if (_this.wait == 0) { $('#hsbtn').rem ...

  3. 34 Flutter仿京东商城项目 用户注册 注册流程 POST发送验证码 倒计时功能 验证验证码

    加群452892873 下载对应34课文件,运行方法,建好项目,直接替换lib目录 以下列出的是本课涉及的文件. RegisterFirst.dart import 'package:flutter/ ...

  4. iOS 短信验证码倒计时按钮的实现

    验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能,点击获取验证码,进入时间倒计时,感兴趣的小伙伴们可以参考一下: 实现思路: 创建按钮,添加点击方法: 用NSTimer定时器, ...

  5. Flutter 实际开发常用工具类(全局提示,请求封装,token缓存,验证码倒计时、常用窗帘动画及布局)

    介绍: 一星期从入门到实际开发经验分享及总结           代码传送门github Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面.未来App开发 ...

  6. iOS “获取验证码”按钮的倒计时功能

    iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispatch_source_set_timer方法去更加方便的使用计时器.我们也可以很容易的的 ...

  7. iOS 按钮倒计时功能

    iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...

  8. javascript的倒计时功能中newData().getTime()在iOS下会报错问题解决

    javascript的倒计时功能中newData().getTime()在iOS下会报错问题解决 在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间,而在i ...

  9. 微信小程序发送验证码功能,验证码倒计时

    data{ timer:'', countDownNum:'发送验证码', } // 点击验证码倒计时获取验证码 Gain:function(e){ let that = this let count ...

随机推荐

  1. 分类算法之朴素贝叶斯分类(Naive Bayesian Classification)

    1.什么是分类 分类是一种重要的数据分析形式,它提取刻画重要数据类的模型.这种模型称为分类器,预测分类的(离散的,无序的)类标号.例如医生对病人进行诊断是一个典型的分类过程,医生不是一眼就看出病人得了 ...

  2. Codevs No.1287 矩阵乘法

    2016-06-01 16:53:23 题目链接: 矩阵乘法 (Codevs No.1287) 题目大意: 给你两个可乘矩阵a,b,求a*b 解法: 定义....... //矩阵乘法 (Codevs ...

  3. J2EE到底是什么

    目前所有的B/S系统应用可以分为:有状态(statefull)和无状态(stateless)两大类别. 有状态是指在整个系统的处理过程中要保留记住一些信息,而无状态则相反,每次request都是独立的 ...

  4. Android 用MediaCodec实现视频硬解码(转)

    本文向你讲述如何用android标准的API (MediaCodec)实现视频的硬件编解码.例程将从摄像头采集视频开始,然后进行H264编码,再解码,然后显示.我将尽量讲得简短而清晰,不展示 那些不相 ...

  5. 【转】 Nginx系列(一)--nginx是什么?

    原博文出于:http://blog.csdn.net/liutengteng130/article/details/46700939  感谢! 一.介绍 Nginx是一个高性能的HTTP和反向代理服务 ...

  6. document.documentElement.clientHeight||document.documentElement.scrollHeight

    在我看<JavaScript高级程序设计>(第三版)的时候,在clientHeight和scrollHeight那部分把我弄糊涂了. 原书是这样写的:( //宽度同理,就不仔细描述了.) ...

  7. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  8. 159. Longest Substring with At Most Two Distinct Characters

    最后更新 二刷 08-Jan-17 回头看了下一刷的,用的map,应该是int[256]的意思,后面没仔细看cuz whatever I was doing at that time.. wasnt ...

  9. iOS-default.png启动图片

    我在xcode5下写的代码,我下载了iOS6的模拟器,我用iOS6和iOS7的模拟器切换运行,有的时候可以运行有的时候不可以运行,报错: 2013-11-17 16:49:04.049 sim[474 ...

  10. [1.1]Knowledge that should be prepared

    Actually, there are a huge amount of knowledge we need to learn. So I hope you don't be scared. It's ...