最近一个项目有获取手机短信跟邮箱验证码功能, 所以要加一个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. ueditor问题简记录

    一.百度ueditor下载地址:http://ueditor.baidu.com/website/download.html. uBuilder下载,个人选了一些自用的,.net的,但是很奇怪下载响应 ...

  2. K/3 Cloud开发之旅 -- 主页自定义篇(一)

    如果说我们要进行主页自定义篇,首先涉及到的就是登陆的自定义,那么如何进行登录界面的自定义呢 其实登陆界面自定义主要就是图片的替换 ,那么我们就看下登陆界面的图片的组成 登录页面底图有两部分组成,一个是 ...

  3. springboot maven install 找不到符号

    好多朋友在网上找maven install 找不到符号,我今天也遇到了同样的问题,我项目结构如下: 在multicreate-web这个项目引用了multicreate-service的jar包,在i ...

  4. elasticsearch-查询

    使用如下语句创建一个名字为:user_document,别名为user的索引 PUT:http://localhost:9200/user_document { "settings" ...

  5. 算法入门笔记------------Day2

    1.开灯问题 有n盏灯,编号为1-n,第一个人把所有灯打开,第二个按下所有编号为2的倍数的开关(这些灯都被关掉),第三个人按下所有编号为3的倍数的开关,依次类推,一共有k个人,问最后有哪些灯开着?输入 ...

  6. col-md-*,col-xs-*

    class属性 col-md-*,md 表示 medium (中等的),* 代表一个数字,它指定了这个元素所占的列宽. 在中等大小的屏幕上(例如笔记本电脑),元素的列宽被指定了. col-xs-* , ...

  7. Access 2003下载地址和密钥

    电驴下载链接:ed2k://|file|sc_access_2003.iso|645523456|63AA6C30D609FDC22D056C4B424283F9|/ 安装SN: HH74C-P8F7 ...

  8. Object-C中一些不同于C系列语言表现的特性

    这段时间体验和学习OC,虽然这么多年基本都在使用C系列语言(C,C++,C#),但是仍然有很多的不习惯. 当然,这些不习惯不代表讨厌或者不好,也许这些就是OC作为Apple开发首选语言而显得特殊的一些 ...

  9. android camera 自定义开发

    1.检测是否有摄像头 /** Check if this device has a camera */ private boolean checkCameraHardware(Context cont ...

  10. U盘的不识别问题

    1.案例: 我重新装过电脑以后出现过一个问题是: 我的U盘只能在我电脑上用,在其他电脑上不能用 其他人的U盘不能在我的电脑上用. 2.根本问题: 是驱动问题.接口上没符合的驱动,未能指定路径.电脑上可 ...