ios. GCD 倒计时时间
//倒计时时间
__block int timeout = 60;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//创建globle队列
//创建timer 定时器
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//每秒执行。 设置1s触发一次,0s的误差
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 源
dispatch_async(dispatch_get_main_queue(), ^{
//回主线程 更新UI 设置界面的按钮显示 根据自己需求设置
weakSelf.getCodeBtn.enabled = YES;
[weakSelf.getCodeBtn setTitle: @"获取验证码" forState:UIControlStateNormal];
});
}
else
{
int seconds = timeout % 60;
NSString *strTime = [NSString stringWithFormat:@"%.0d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
//回主线程 更新UI 设置界面的按钮显示 根据自己需求设置
weakSelf.getCodeBtn.enabled = NO;
weakSelf.getCodeBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
if (strTime.length == 0) {
[self.getCodeBtn setTitle:[NSString stringWithFormat:@"验证码(60)"] forState:UIControlStateDisabled];
}
else
{
[self.getCodeBtn setTitle:[NSString stringWithFormat:@"验证码(%@)",strTime] forState:UIControlStateDisabled];
}
});
timeout--;
}
});
//开始执行dispatch源
dispatch_resume(_timer);
ios. GCD 倒计时时间的更多相关文章
- iOS GCD倒计时
GCD倒计时的好处在于不用考虑是否定时器无法释放的问题,runloop的问题,还有精度更加高 使用GCD创建定时器方法 -(void)startCountDown:(NSInteger)maxTime ...
- iOS验证码倒计时(GCD实现)
+ (void)verificationCode:(void(^)())blockYes blockNo:(void(^)(id time))blockNo { __block ; //倒计时时间 d ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
- iOS中倒计时
方法一:使用NSTimer来实现(比较适用于发送短信验证码倒计时) 主要是利用NSTimer的scheduledTimerWithTimeInterval方法来每秒执行一次changeTime方法 / ...
- GCD 倒计时
今天在Code4App上看了一个GCD倒计时的Demo,觉得不错代码贴出来备用 -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t queue = ...
- iOS GCD之dispatch_semaphore(信号量)
前言 最近在看AFNetworking3.0源码时,注意到在 AFURLSessionManager.m 里面的 tasksForKeyPath: 方法 (L681),dispatch_semapho ...
- iOS GCD 拾遗
GCD里就有三种queue(分派队列)来处理. 1. Main queue:(主队列) 顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Qu ...
- ios实现倒计时的两种方法
方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMeth ...
- ecshop修改产品详情 折扣倒计时时间
文件:lefttime.js 位置:/js/lefttime.js 要求:去掉倒计时时间的 “天”数 原代码 ) { Temp = dateLeft + _day + hourZero + hour ...
随机推荐
- 怎样使用Fiddler获取WebApi的token值?
User-Agent: Fiddler Host: localhost: Content-Length: Content-Type: application/json grant_type=passw ...
- java 中文及特殊字符校验
java 中文及特殊字符校验 CreateTime--2017年8月25日16:54:50 Author:Marydon 一.参考链接 http://blog.csdn.net/imduan/ar ...
- 【DB2】慎用nickname,可能会引起效率较低
在使用db2的时候,在A库建立了nickname,指向B库的物理表(数据量800多万),现在使用程序(JDBC)连接数据库,并计算指标,但是发现效率很低. 解决办法: 删除A库中的nickname,在 ...
- 亲历H5移动端游戏微信支付接入及那些坑(三)——支付接入
终于到接入支付了,小小的一个微信支付,居然也写了3篇,好长,好累. 接入环境 对接入环境,前端的话,应该是以js为主吧,也有可能是,PHP,Java,C++,或者C#都可以.为什么在此特意提一下接入环 ...
- Ubuntu x86-64汇编(5) 控制指令
控制指令 Control Instructions 汇编的控制指令主要包含标签, 无条件跳转和有条件跳转几种 标签 Label 标签用于标记跳转的目的, 必须以字母开头, 后面跟着字母, 数字和下划线 ...
- macos下安装oh-my-zsh和zsh-autosuggestion
1:安装oh-my-zsh sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/mast ...
- 【php将日志写入一个按时间天来分类的文件】put_file_contents()
具体参考php官方文档:http://php.net/manual/zh/function.file-put-contents.php 具体参数:成功返回插入数据的字节数,失败返回false 使用示例 ...
- yum安装nodejs 8
Install Node.js 8 on Enterprise Linux Distributions To install Node.js 8 on Enterprise Linux distros ...
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
保存实体异常 https://blog.csdn.net/zzzz3621/article/details/9776539 org.hibernate.NonUniqueObjectException ...
- hibernate实现多表联合查询
转自:http://blog.sina.com.cn/s/blog_67b9ad8d01010by1.html 以前用sql实现联合查询 是非常简单的事,只需要写sql语句就可以,第一次遇到hiber ...