GCD实现倒计时
之前面试中,好多面试官,问使用GCD如何实现倒计时,我当时也没写过,所以一时不知道怎么说,所以结束之后,我实现一下GCD的倒计时。
- (void)startTime:(UIButton *)sender {
__block int timeout=60; //倒计时时间
//创建队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
[sender setTitle:@"重新获取" forState:UIControlStateNormal];
sender.userInteractionEnabled = YES;
});
}else{
int seconds = timeout % 61;
NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[sender setTitle:[NSString stringWithFormat:@"发送中(%@秒)",strTime] forState:UIControlStateNormal];
[UIView commitAnimations];
sender.userInteractionEnabled = NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}
github中demo: https://github.com/ZY201611/ZPStrikeLine
GCD实现倒计时的更多相关文章
- 使用 GCD 实现倒计时效果
效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @p ...
- iOS 使用GCD实现倒计时效果
在APP开发过程中,经常有需要实现倒计时效果, 比如语音验证码倒计时...代码如下: __block int timeout = 100; dispatch_queue_t queue = dispa ...
- ios 使用gcd 显示倒计时
__block ;//倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ...
- 使用GCD验证码倒计时
__block int timeout = 60; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY ...
- 倒计时实现两种方法-NSTimer/GCD
#import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
- 使用系统自带的GCD的timer倒计时模板语句遇到的小坑。。
今天折腾了下系统gcd的 但是如果不调用这句dispatch_source_cancel()那么这个timer根本不工作....解决方法如下: 实现一个倒计时用自带的gcd如此简洁.. 原因可能是如果 ...
- GCD 倒计时
今天在Code4App上看了一个GCD倒计时的Demo,觉得不错代码贴出来备用 -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t queue = ...
- 利用GCD 中的 dispatch_source_timer 给tableViewCell添加动态刷新的计时/倒计时功能
1.思路一(失败) 在设置好cell 里的内容之后在每个cell 返回时调用定时器事件,更新cell 内容,然后刷新整个表格. - (void)didadida:(UITableViewCell *) ...
随机推荐
- JavaScript var的作用域和提升
在ES6标准之前,var 作为唯一的声明变量关键字,本篇将着重介绍var的作用域和变量提升. 1. var Hoisting(变量提升) va rHoisting:使用var在函数或全局内任何地方声明 ...
- 在Tomcat中采用基于表单的安全验证
.概述 (1)基于表单的验证 基于From的安全认证可以通过TomcatServer对Form表单中所提供的数据进行验证,基于表单的验证使系统开发者可以自定义用户的登陆页面和报错页面.这种验证方法 ...
- 在 ios 中的日期格式
var d="2017-1-1" ; new Date(d) //生成一个日期对象 这样写在 Android 中没有问题,但是在 ios 中,d 的格式不对,应该设为 2017- ...
- 豹哥嵌入式讲堂:ARM知识概要杂辑(4)- Cortex-M处理器性能指标
1.处理器的性能指标 用于评价CPU的性能指标非常多,不同的性能侧重点下的测试标准可能得出的指标值不同,下面介绍嵌入式行业广泛使用的两个经典的测试标准. 1.1 Dhrystone标准 Dhrysto ...
- CSS 自动隐藏文字并添加省略号
.cut { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; -o-text-overflow:ellipsis; -icab ...
- convert图像格式批量转换
问题:利用GMT绘制生成了eps格式的图像,为了将图像插入到word中,且保持较高的分辨率,利用convert进行图像格式转换,将eps转换成tiff格式. code: $i ${name}.tif ...
- 深入浅出Hadoop之HDFS
hadoop生态系统一直是大数据灵域的热点,其中包括今天要聊的HDFS,和计划以后想聊的yarn, mapreduce, spark, hive, hbase, 已经聊过的zookeeper,等等. ...
- c#代码启动任务管理器的几种方法
1.直接启动 ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Path.Combine(Environment.GetE ...
- ldconfig几个需要注意的地方
1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library会找不到 2. 想往上面两个目录以外加东西的时候, ...
- vue2使用高德地图vue-amap定位以及AMapUI标注
前言 最近在vue里使用了高德地图vue-amap以及AMapUI,我在这里就说下如何在vue2里引入vue-amap和AmapUI以及使用定位 (在这里默认你已经安装了vue-cli) 安装 npm ...