UIButton添加倒计时
最近一个项目有获取手机短信跟邮箱验证码功能, 所以要加一个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添加倒计时的更多相关文章
- 使用关联对象(AssociatedObject)为UIButton添加Block响应
在开发中,要给UIButton添加点击事件的话,通常的做法是这样的 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [ ...
- JS实现为控件添加倒计时功能
一.概述 在有些报表需求中,需要为控件添加倒计时功能,限制到某一个时间点后能进行一项操作或不能进行某项操作,比如查询,导出功能等等,又需要人性化地显示还有多少时间,即倒计时功能,比如下图中我们限制这个 ...
- Redux教程3:添加倒计时
前面的教程里面,我们搭建了一个简单红绿灯示例,通过在console输出当面的倒计时时间:由于界面上不能显示倒计时,用户体验并不良好,本节我们就添加一个简单的倒计时改善一下. 作为本系列的最后一篇文章, ...
- iOS开发-UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选择 ...
- IOS UIlabel 、UIButton添加下划线
1.给UILabel 添加下划线 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )]; label.backgrou ...
- UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...
- iOS UIButton添加圆角,添加边框
//准备工作 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(,, ...
- 为 UIButton 添加长按事件
UIButton *aBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; [aBtn setFrame:CGRectMake(40, 100, 60, 6 ...
- cell上添加倒计时,以及时差问题的解决
原址 http://www.cnblogs.com/zhangmaliang/p/5102518.html 最近项目中用到了tableView的多个cell倒计时系统问题,本觉得很简单的一个事,一做发 ...
随机推荐
- yii打印sql
想打印Sql的话,可以用把你要执行的命令例如queryAll(),queryOne(),execute()换成getRawSql(); 例如 : 要看$result = Yii::$app->d ...
- Java 反射工具类封装
封装了常用的反射相关方法 public class ReflectUtil { /** * 通过类路径获取Class * * @author LHY <br> * Description ...
- centos下postgresql的安装与配置[转]
本文摘自:http://blog.chinaunix.net/uid-24846094-id-78490.html 一.安装(以root身份进行) 1.检出最新的postgresql的yum配置从ht ...
- middleware - bodyparser
express4之前,bodyparser是express下的一个对象. express4把bodyparser分离出来. 本文中的实例基于以下的这个请求 $.ajax({ url: '/save', ...
- 通过uCGUIBulider4.0建立的ucGUI文件,控件汉字不能显示问题解决办法
由于uCGUIBulider4.0不能在64位操作系统中运行,于是在电脑上通过VMware Workstation Pro搭建虚拟的32位的win7环境,然后把win7中用uCGUIBulider4. ...
- FMDB读取Datetime类型值为1970的问题
1.问题 今天使用FMDB做一个例子程序,新建的一张表有一个datetime字段,数据库有默认值,大概如下 CREATE TABLE [ConsumptionType] ([id] INTEGER P ...
- 河南省第四届ACM程序设计大赛
A: 序号互换 #include <cstdio> #include <cstdlib> #include <cstring> #include <algor ...
- zoj 3725 - Painting Storages(动归)
题目要求找到至少存在m个连续被染成红色的情况,相对应的,我们求至多有m-1个连续的被染成红色的情况数目,然后用总的数目将其减去是更容易的做法. 用dp来找满足条件的情况数目,, 状态:dp[i][0] ...
- Android Meun 用法
Android Meun 用法 点击菜单实体键弹出菜单:如下图 main_activity.xml <?xml version="1.0" encoding="ut ...
- UNET学习笔记3 - 网络系统的概念
服务器和 HOST 在Unity游戏里,一个游戏一般有一个服务器和多个客户端组成,但也可以没有服务器,用某一个客户端来同时做服务器用,这种就叫Host 在Host上的客户端叫Local Client, ...