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倒计时系统问题,本觉得很简单的一个事,一做发 ...
随机推荐
- 深入理解JavaScript中的==运算符
原文章地址 在详细介绍图1中的每个部分前,我们来复习一下JS中关于类型的知识: JS中的值有两种类型:基本类型.对象类型. 基本类型包括:Undefined.Null.Boolean.Number和S ...
- mysql 5.7开启并行复制
开启多线程复制,默认关键的参数有两个: mysql> show variables like 'slave_parallel_%'; +------------------------+---- ...
- nodejs之异步思想
nodejs的精髓就是"异步",但什么是异步呢?我们来看一个例子: var start =new Date; setTimeout(function(){ var end =new ...
- UE4动作流程总结
右键新窗口打开看大图
- 改变html中鼠标形状
要修改的属性是: style=”cursor:default” 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 pointer:是手型. crosshair:是十字 ...
- Java中HashMap等的实现要点浅析
@南柯梦博客中的系列文章对Jdk中常用容器类ArrayList.LinkedList.HashMap.HashSet等的实现原理以代码注释的方式给予了说明(详见http://www.cnblogs.c ...
- 关于Mysql查询带单引号及插入带单引号字符串问题
1.转为带参数查询 String sql=""select id from student where name='?'; Connection connect = DriverM ...
- 如何编写 Cloud9 JavaScript IDE 的功能扩展
上周末我们在JSConf.eu发布了 Cloud9 IDE ,同时发布了对应的GitHub项目.在4天时间里该项目得到340个人的关注和将近50个fork.Cloud9的口号是由"由Java ...
- Android 各层调用的方式
所有的android的app启动都有三种深入启动的方式: ①app——Runtime Service——Lib 这种启动方式是: Ⅰapp程序中启动application framework 层中Ru ...
- Entity Framework搜索指定字段解决方案
public class Book { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; ...