最近一个项目有获取手机短信跟邮箱验证码功能, 所以要加一个UIButton倒计时功能

例子代码如下:

 //获取验证码按钮
- (IBAction)getButtonClick:(UIButton *)sender; #pragma mark - 获取验证码
- (IBAction)getButtonClick:(UIButton *)sender
{
//正常状态下的背景颜色
UIColor *mainColor = [UIColorcolorWithRed:/.0green:/.0blue:/.0alpha:1.0f];
//倒计时状态下的颜色
UIColor *countColor = [UIColorlightGrayColor];
[selfsetTheCountdownButton:sender startWithTime:5title:@"获取验证码"countDownTitle:@"s"mainColor:mainColor countColor:countColor];
} #pragma mark - button倒计时
- (void)setTheCountdownButton:(UIButton *)button startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle mainColor:(UIColor *)mColor countColor:(UIColor *)color {
//倒计时时间
__block NSInteger timeOut = timeLine;
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,, , queue);
//每秒执行一次
dispatch_source_set_timer(_timer,dispatch_walltime(NULL,), 1.0 * NSEC_PER_SEC,);
dispatch_source_set_event_handler(_timer, ^{

//倒计时结束,关闭
if (timeOut == ) {
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
button.backgroundColor = mColor;
[button setTitle:titleforState:UIControlStateNormal];
button.userInteractionEnabled =YES;
});
} else {
int seconds = timeOut % ;
NSString *timeStr = [NSStringstringWithFormat:@"%0.1d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
button.backgroundColor = color;
[button setTitle:[NSStringstringWithFormat:@"%@%@",timeStr,subTitle]forState:UIControlStateNormal];
button.userInteractionEnabled =NO;
});
timeOut--;
}
});
dispatch_resume(_timer);
}

UIButton添加倒计时的更多相关文章

  1. 使用关联对象(AssociatedObject)为UIButton添加Block响应

    在开发中,要给UIButton添加点击事件的话,通常的做法是这样的 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [ ...

  2. JS实现为控件添加倒计时功能

    一.概述 在有些报表需求中,需要为控件添加倒计时功能,限制到某一个时间点后能进行一项操作或不能进行某项操作,比如查询,导出功能等等,又需要人性化地显示还有多少时间,即倒计时功能,比如下图中我们限制这个 ...

  3. Redux教程3:添加倒计时

    前面的教程里面,我们搭建了一个简单红绿灯示例,通过在console输出当面的倒计时时间:由于界面上不能显示倒计时,用户体验并不良好,本节我们就添加一个简单的倒计时改善一下. 作为本系列的最后一篇文章, ...

  4. iOS开发-UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...

  5. IOS UIlabel 、UIButton添加下划线

    1.给UILabel 添加下划线 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )]; label.backgrou ...

  6. UILabel和UIButton添加下划线

    关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...

  7. iOS UIButton添加圆角,添加边框

    //准备工作 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(,, ...

  8. 为 UIButton 添加长按事件

    UIButton *aBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; [aBtn setFrame:CGRectMake(40, 100, 60, 6 ...

  9. cell上添加倒计时,以及时差问题的解决

    原址 http://www.cnblogs.com/zhangmaliang/p/5102518.html 最近项目中用到了tableView的多个cell倒计时系统问题,本觉得很简单的一个事,一做发 ...

随机推荐

  1. http statusCode(状态码) 200、300、400、500序列

    201-206都表示服务器成功处理了请求的状态代码,说明网页可以正常访问.        200(成功)  服务器已成功处理了请求.通常,这表示服务器提供了请求的网页.        201(已创建) ...

  2. iOS基础之Xcode 8相关

    1.屏蔽日志输出 2.注释相关 注释不能使用:命令运行:  sudo /usr/libexec/xpccachectl  VVDocument方式注释快捷键:option + command + /

  3. linux下安装oracle

    一>1.关闭防火墙,禁用selinux vi /etc/selinux/config  修改SELINUX=disabled,然后重启,如果不想重启使用命令setenforce 0 2.安装依赖 ...

  4. JDBC小工具--TxQueryRunner及其单元测试

    1.TxQueryRunner的简介(需要相关jar包的请留言) TxQueryRunner类是common-dbutils下QueryRunner的子类,是用来简化JDBC操作的,所以要导入comm ...

  5. 20151013 C# 第一篇 流程控制语句

    20151013 流程控制语句: 1.选择语句 if … else if...else 选择语句 if(布尔表达式){  语句块:} if(布尔表达式){  语句块1:}else{  语句块2:} i ...

  6. block的复习

    main.m // //  main.m //  8A10.Block的复习 // //  Created by huan on 16/2/8. //  Copyright © 2016年 huanx ...

  7. Python排列组合问题

    1.字符串的全排列 问题描述:打印出原字符串中所有字符的所有排列.——将输入字符串中的每个字符作为一个不同的字符看待,即使它们是重复的,如'aaa'应打印6次. Python可以用生成器解决: def ...

  8. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  9. Global文件编译发布,代码不执行的问题与解决

    问题:在Application_BeginRequest添加防止跨站点注入的过滤代码,VS2005编译成DLL发布后发现代码不会被执行: 环境:windows server 2008 r2 x64位  ...

  10. 获取基于Internet Explorer_Server的聊天窗口内容

    假设在得到窗体中控件的句柄(通过SPY++)的前提下,如果是像文本框这种控件,只要用SendMessage就可得到文本了,但是对于聊天记录窗口却行不通(返回空值),因为那其实是一个内置浏览器Inter ...