GCD倒计时的好处在于不用考虑是否定时器无法释放的问题,runloop的问题,还有精度更加高

使用GCD创建定时器方法

-(void)startCountDown:(NSInteger)maxTime{
__block int time = 0;
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);
//如果有需要可以将定时器保存为全局变量
_timer = timer;
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
if (time >= maxTime) { //时间到了
//清空计数
time = 0;
//时间到了关闭定时器
dispatch_source_cancel(timer)
dispatch_async(dispatch_get_main_queue(), ^{
//在这里执行你要的操作
do something...
});
}else{
time++;
}
});
dispatch_resume(timer);
}

iOS GCD倒计时的更多相关文章

  1. ios. GCD 倒计时时间

    //倒计时时间 __block int timeout = 60; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_ ...

  2. iOS活动倒计时的两种实现方式

    代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...

  3. GCD 倒计时

    今天在Code4App上看了一个GCD倒计时的Demo,觉得不错代码贴出来备用 -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t queue = ...

  4. iOS 按钮倒计时功能

    iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...

  5. iOS GCD基础篇 - 同步、异步,并发、并行的理解

    1.关于GCD - GCD全称是Grand Central Dispatch  - GCD是苹果公司为多核的并行运算提出的解决方案  - GCD会自动利用更多的CPU内核(比如双核.四核)  - GC ...

  6. iOS GCD之dispatch_semaphore(信号量)

    前言 最近在看AFNetworking3.0源码时,注意到在 AFURLSessionManager.m 里面的 tasksForKeyPath: 方法 (L681),dispatch_semapho ...

  7. iOS GCD 拾遗

    GCD里就有三种queue(分派队列)来处理. 1. Main queue:(主队列) 顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Qu ...

  8. iOS验证码倒计时(GCD实现)

    + (void)verificationCode:(void(^)())blockYes blockNo:(void(^)(id time))blockNo { __block ; //倒计时时间 d ...

  9. iOS GCD 编程小结

    一.简单介绍 1.GCD简介? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD优势 GCD是苹果公司为多核的并行运算提出的 ...

随机推荐

  1. Frequency

    题目描述 Snuke loves constructing integer sequences. There are N piles of stones, numbered 1 through N. ...

  2. 【bzoj1562】【[NOI2009]变换序列】匈牙利算法的性质利用

    (上不了p站我要死了,侵权度娘背锅) Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30 ...

  3. iOS9 Storyboard unwind segue反回传递事件时机详细步骤

    当返回上一个界面且需要上一个界面做某事时,用unwind segue实现起来比delegate简单许多,甚至有时不适合用delegate来实现,那么我们就用unwind segue吧,而且像1-> ...

  4. Visual Studio 插件开发资源

    微软官方MSDN 官方MSDN永远是最大而全的电子字典Visual Studio Software Development Kit ,不过它的资料虽然详细,但没有一定的基础的话直接使用它的话有点无从入 ...

  5. Qt creator发布可执行文件方式----靠谱

    1.首先用 QtCreator 新建一个 Qt Widgets Application 项目,直接用默认的 QMainWindow 程序就可以了,项目名字假定是serial_port.exe. 然后以 ...

  6. ES6里关于字符串的拓展

    一.子串识别 自从 JS 引入了 indexOf() 方法,开发者们就使用它来识别字符串是否存在于其它字符串中.ES6 包含了以下三个方法来满足这类需求: 1.includes():该方法在给定文本存 ...

  7. python git log

    # -*- coding: utf-8 -*- # created by vince67 Feb.2014 # nuovince@gmail.com   import re import os imp ...

  8. HDFS删除并清空回收站

    删除文件并放入回收站: hdfs dfs -rm -f /path 删除文件不放入回收站: hdfs dfs -rm -f -skipTrash /path 清空回收站: hdfs dfs -expu ...

  9. 数据库中存在0,1,2.....或者1,null,2 排序时让0或者null在最后的sql语句

     select * from yryz_products_t order by isnull(sort),sort;     select * from yourtable order by cast ...

  10. Linux Barrier I/O 实现分析与barrier内存屏蔽 总结

    一直以来.I/O顺序问题一直困扰着我.事实上这个问题是一个比較综合的问题,它涉及的层次比較多,从VFS page cache到I/O调度算法,从i/o子系统到存储外设.而Linux I/O barri ...