iOS实战——GCD实现时间倒数
来源:互联网 发布:服务器网络拓扑图图片 编辑:程序博客网 时间:2024/06/02 21:44
- UILabel显示倒数
/** * 倒计时GCD通用方法 * 通常用的计时器都是用NSTimer,但是NSTimer在线程很吃紧的时候效果不佳,使用GCD计时相对更好 * * @param seconds 倒计时间 单位:秒 * @param showLable 需要显示的文本框 * @param endBlock 倒计时结束后,回调的Block */+ (void)startTimerWithSeconds:(long)seconds showLable:(UILabel *)showLable endBlock:(void (^)())endBlock{ [self startTimerWithSeconds:seconds showLable:showLable strFormat:nil endBlock:endBlock];}
调下如下:
[BSTool startTimerWithSeconds:remainTime showLable:self.countdownLable endBlock:^{ self.countdownLable.text = @"订单超时"; [weakSelf.tableView reloadData]; }];
- UILabel 格式显示倒数,并且倒数完成后需要进行另外的操作,则需要用block回调
/** * 倒计时GCD通用方法 * 通常用的计时器都是用NSTimer,但是NSTimer在线程很吃紧的时候效果不佳,使用GCD计时相对更好 * * @param seconds 倒计时间 单位:秒 * @param strFormat 格式化样式,如 "剩%@自动关闭" * @param showLable 需要显示的文本框 * @param endBlock 倒计时结束后,回调的Block */+ (void)startTimerWithSeconds:(long)seconds showLable:(UILabel *)showLable strFormat:(NSString *)format endBlock:(void (^)())endBlock{ __block long timeout = seconds; // 倒计时时间 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){ // 倒计时结束,回调block dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ if (endBlock) { endBlock(); } }); } else{ NSString *strTime = [NSString stringWithFormat:@"%02ld分%02ld秒",(long)(timeout % 3600 / 60), (long)(timeout % 60)]; //回到主界面,显示倒计时 dispatch_async(dispatch_get_main_queue(), ^{ if (format) { // 判断是否要格式化 showLable.text = [NSString stringWithFormat:format,strTime]; } else { showLable.text = strTime; } }); timeout--; } }); dispatch_resume(_timer);}
调用如下:
[BSTool startTimerWithSeconds:payResultModel.remainTime showLable:self.payStateDescLabel strFormat:@"剩%@自动关闭" endBlock:^{ self.payStateDescLabel.text = @"交易关闭"; }];
- 获取短信验证码按钮倒数
// MARK:倒计时- (void)setTimer { __block int timeout = 60; 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(), ^{ sendCode.userInteractionEnabled = YES; [sendCode setTitle:@"获取验证码" forState:UIControlStateNormal]; [sendCode setTitleColor:Color_A1A1A1 forState:UIControlStateNormal]; }); } else { int seconds = timeout % 61; NSString *strTime = [NSString stringWithFormat:@"%.2d",seconds]; dispatch_async(dispatch_get_main_queue(), ^{ [sendCode setTitleColor:Color_A1A1A1 forState:UIControlStateNormal]; [sendCode setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal]; sendCode.userInteractionEnabled = NO; }); timeout--; } }); dispatch_resume(_timer);}
0 0
- iOS实战——GCD实现时间倒数
- iOS开发—使用GCD实现多线程
- iOS学习笔记10(5)—GCD实战一:使用串行队列实现简单的预加载
- iOS—GCD使用方法
- iOS学习笔记10(6)—GCD实战2:资源竞争
- iOS项目开发实战——获取系统当前时间
- iOS GCD实现
- iOS GCD实现
- iOS多线程——GCD
- iOS多线程——GCD
- iOS - 多线程——GCD
- IOS开发—GCD介绍
- ios学习路线—iOS高级(GCD)
- IOS多线程之——GCD。
- IOS多线程之——GCD(二)
- IOS多线程之——GCD总结。
- iOS —— 多线程之GCD
- iOS分享【OC】—— 浅谈 GCD
- js判断某个数字是否为小数 并且有几位小数
- posix pthread 使用笔记
- 多线程一
- 1003: [ZJOI2006]物流运输
- 找工作之STL源码
- iOS实战——GCD实现时间倒数
- Zoj 3537 Cake (DP_最优三角形剖分)
- Codeforces Beta Round #8 E. Beads(数位DP)
- 购物车DEMO
- 深入理解javascript原型和闭包
- 全局变量与强符号和弱符号
- 金融类期刊
- RPC框架之-DUBBO(二)负载均衡
- 使用JMeter创建数据库(Mysql)测试