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 ...
随机推荐
- 单页面SPA应用路由原理 history hash
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 3、redis之java client环境搭建
JAVA Client环境搭建 POM: <dependency> <groupId>redis.clients</groupId> <artifactId& ...
- django之创建第11个项目-页面整合
目的:将如下众多html页面整合到一个index.html页面中. 百度云盘:django之创建第11个项目-页面整合 用下面的方式实现: <!DOCTYPE html> <head ...
- Linux针对缺少响应xxx.so.xxx解决方案
问题描述: 在yum安装,偶尔会出现缺少xxx.so.0 xxxx.so.1等类似问价,大部分都是缺失对应的库文件 --> Processing .5p1-.fc27.x86 _64--> ...
- iOS12 Network框架 自签名证书认证
发布时间:2018-09-21 技术:iOS12 xcode10 golang1.11 概述 iOS12 苹果发布了新的网络框架Network,可以更方便地操作底层网络通信了.使用TLS也很方 ...
- mac下phpstrom安装主题和主题推荐
phpstorm主题 在mac下finder下command+shift+G键到~/Library/Preferences/PhpStorm2016.2/colors,我的路径可能和你的不同,但是大同 ...
- android布局layout中的一些属性
1.可以使某些资源文件或UI组件可重用 <include layout="@layout/other"/> 2.定义一个文本编辑框,使用绝对定位 android: ...
- ThinkPHP+jQuery EasyUI Datagrid查询数据的简单处理
ThinkPHP和jQuery EasyUI这两个都是不错的框架,现在要把它两个整合到一块,做个简单的Ajax调用查询. 在ThinkPHP模板中引入EasyUI的相关文件,然后设置按钮3的调用: & ...
- 数据结构学习之stack
不能小看这些基本的数据结构,写了才发现还是会有问题出现的. 有码有真相: #pragma once class MyStack { public: MyStack(void); ~MyStack(vo ...
- linux下常用文件传输命令(转)
因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...