博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCD之定时器dispatch_source_t(转载暂时未完全理解)
阅读量:6934 次
发布时间:2019-06-27

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

#import "ViewController.h"@interface ViewController (){    IBOutlet UIButton *l_timeButton;}@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    [l_timeButton addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside];}-(void)startTime{    __block int timeout=30; //倒计时时间    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(), ^{                //设置界面的按钮显示 根据自己需求设置                [l_timeButton setTitle:@"发送验证码" forState:UIControlStateNormal];                l_timeButton.userInteractionEnabled = YES;            });        }else{            //            int minutes = timeout / 60;            int seconds = timeout % 60;            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];            dispatch_async(dispatch_get_main_queue(), ^{                //设置界面的按钮显示 根据自己需求设置                NSLog(@"____%@",strTime);                [l_timeButton setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];                l_timeButton.userInteractionEnabled = NO;                            });            timeout--;                    }    });    dispatch_resume(_timer);    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

转载地址:http://txljl.baihongyu.com/

你可能感兴趣的文章
2012-09-03 → 2012-09-09 周总结
查看>>
通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题
查看>>
.net调试插件sosex的mk命令显示调用堆栈
查看>>
.NET开发者可以在Windows 8中使用ARM
查看>>
【C#】隐式类型var
查看>>
关于Jquery中ajax方法data参数用法的总结
查看>>
hdu 1496(hash经典)
查看>>
javascript转换日期字符串为Date对象
查看>>
javascript 兼容不同手机的 canvas
查看>>
一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少?
查看>>
方法javaJVM学习笔记-内存处理
查看>>
android官方资料
查看>>
贴片电感和贴片磁珠的区别
查看>>
ArcGIS与SuperMap的使用比较(1)
查看>>
Android开发1——查找所需要出示权限的内容
查看>>
C#/Net代码精简优化技巧
查看>>
wget
查看>>
如何查看自己电脑系统的安装日期-Window上
查看>>
【Java】IO Stream详细解读
查看>>
Linux C 读取文件夹下所有文件(包括子文件夹)的文件名
查看>>