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

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

@property (nonatomic, assign) NSInteger timeout;

 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100, 100, 200, 100);

    [button setTitle:@"跳转" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];

    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

 

- (void)click:(UIButton *)btn{

    [self countdownButton:btn timeout:59];

}

- (void)countdownButton:(UIButton*)button timeout:(NSInteger)totalTime

{

    //    __block int timeout = totalTime;

    _timeout = totalTime;

    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(), ^{

                button.selected = NO;

                button.userInteractionEnabled = YES;

            });

        }

        else {

            int seconds = _timeout % 60;

            button.selected = YES;

            button.userInteractionEnabled = NO;

            

            NSString *strTime = [NSString stringWithFormat:@"%d 秒后重新获取",seconds];

            dispatch_async(dispatch_get_main_queue(), ^{

                [button setTitle:strTime forState:UIControlStateSelected];

            });

            _timeout--;

        }

    });

    dispatch_resume(_timer);

}

转载于:https://www.cnblogs.com/h-tao/p/5156915.html

你可能感兴趣的文章
MySQL 数据备份与还原
查看>>
Android Camera2 参数调节关键字翻译集合,常用关键字解析
查看>>
NSMutableString 常用操作
查看>>
php-Mysql示例1
查看>>
python第三方库requests学习笔记
查看>>
Oracle基本查询语言
查看>>
Word直接发表博客测试
查看>>
sublime text2 中Emmet常用的技巧 和快捷键
查看>>
浏览器对应用程序的根URL发出请求时所发生的情况(结合 DI)
查看>>
delete和delete[] 的区别
查看>>
[ACM] hdu 2177 取(2堆)石子游戏(威佐夫博弈)
查看>>
3.5 爬虫身份识别与实现网络爬虫技术语言
查看>>
python的dict,set,list,tuple应用详解
查看>>
centos7使用传统网卡名
查看>>
Sublime Text 2 增加python版本
查看>>
spring mvc dispatcherservlet处理request流程
查看>>
怎样加快master数据库的写操作?分表原则!将表水平划分!或者添加写数据库的集群...
查看>>
Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第2节: FastThreadLocal的set方法...
查看>>
C# 程序之间传参数,Args 接收参数的处理
查看>>
Question
查看>>