iOS UI布局-定时器
2015-11-26 19:35
501 查看
定时器是比较常用的一个UI控件,在付款、抢购时,经常会使用到。提取成一个通用的方法
调用时就很简单了
效果:
源代码下载:http://pan.baidu.com/s/1mgKrGZA
/** * 倒计时GCD通用方法 * 通常用的计时器都是用NSTimer,但是NSTimer在线程很吃紧的时候效果不佳,使用GCD计时相对更好 * * @param seconds 倒计时间 单位:秒 * @param showLable 需要显示的文本框 * @param endBlock 倒计时结束后,回调的Block */ - (void)startTimerWithSeconds:(long)seconds showLable:(UILabel *)showLable 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(), ^{ showLable.text = strTime; }); timeout--; } }); dispatch_resume(_timer); }
调用时就很简单了
[self startTimerWithSeconds:1800 showLable:self.timerLabel endBlock:nil];
效果:
源代码下载:http://pan.baidu.com/s/1mgKrGZA
相关文章推荐
- org.hibernate.PropertyValueException: not-null property references a null or transient value:
- UIscrollview
- String、StringBuffer与StringBuilder之间区别
- 读书笔记--UI组件三
- Druid
- GenomicRangeQuery
- IOS开发限制UITextField只可以输入数字
- LayoutInflater的inflate方法实例_null,false,true区别_groupview root是什么
- Programatically Adding Ajax Actions to UIComponents
- 给UILabel局部文字添加颜色
- 052 - N-Queens II
- 051 - N-Queens
- Form Data 转换成 Request Palyload
- Storm UI启动却无法显示
- [UIViewController _loadViewFromNibNamed:bundle:] loaded the "One" nib but the view outlet was not se
- 进击的KFC:UI(九)UITableView 表示图
- IOS之UI -- 按钮UIButton的细节
- iOS开发UI篇—实现UItableview控件数据刷新
- easyui datagrid 的使用
- Machine Learning week 7 quiz: Unsupervised Learning