博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCD倒计时
阅读量:4592 次
发布时间:2019-06-09

本文共 829 字,大约阅读时间需要 2 分钟。

 

@property(nonatomic,retain) dispatch_source_t timer;

 

__block int timeout = 5; //倒计时时间 自定义

    __weak __typeof(&*self)weakself = self;  //防止引用,先弱下

    if (timeout!=0) {

        //GCD定时器

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        _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); //每1秒执行

        dispatch_source_set_event_handler(_timer, ^{

            if(timeout<=0){ //倒计时结束,关闭

                dispatch_source_cancel(_timer);

                _timer = nil;

                dispatch_async(dispatch_get_main_queue(), ^{

                    NSLog(@"倒计时结束操作");

                });

            }else{

                dispatch_async(dispatch_get_main_queue(), ^{

                  NSLog(@"这里显示倒计时时间==%d",timeout);

                });

                timeout--;

            }

            

        });

        dispatch_resume(_timer);

        

    }

 

//退出页面

    dispatch_source_cancel(_timer);

转载于:https://www.cnblogs.com/liaolijun/p/7772378.html

你可能感兴趣的文章
嵌套事务及分类1
查看>>
团队作业2
查看>>
leetcode 437. 路径总和 III
查看>>
hdoj - 1342 Lotto
查看>>
ued.taobao.com
查看>>
香港身份证
查看>>
(二)Python selenium
查看>>
7.装饰器的一些需求
查看>>
优雅就一个字——设计模式之数据上传接口
查看>>
js 数组中sort方法存在的问题
查看>>
Machine Learning - 第7周(Support Vector Machines)
查看>>
Zookeeper注册节点的掉线自动重新注册及测试方法
查看>>
【SVM】清晰明了的理论文章
查看>>
C#学习笔记_02_数据类型
查看>>
Flutter实战视频-移动电商-40.路由_Fluro的全局注入和使用方法
查看>>
ddd
查看>>
Excel 2013中设置密码保护表格数据不被修改的方法
查看>>
Flash中的隔离沙箱
查看>>
即点即用:在 21 世纪交付 Office
查看>>
AJAX表单提交以及数据接收
查看>>