当前位置: 首页 > news >正文

GCD 和Timer

GCD中 进行页面切换的时候 主线程一直刷新倒计时

 

 __block int timeout = 2400; //倒计时时间

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

                //设置界面的按钮显示 根据自己需求设置

                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"即将送达"delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];

                [alert show];

            });

        }else{

            int minutes = timeout / 60;

            NSString * strTime = [NSString stringWithFormat:@"%d",minutes];

            dispatch_async(dispatch_get_main_queue(), ^{

                //设置界面的按钮显示 根据自己需求设置

                UIView * timeView = [[UIView alloc]initWithFrame:CGRectMake(10, 140, 150, 150)];

                timeView.layer.cornerRadius = 75;

                timeView.layer.borderWidth = 4;

                timeView.layer.borderColor = [UIColor colorWithRed:0.816 green:0.820 blue:0.824alpha:1.000].CGColor;

                timeView.backgroundColor = [UIColor whiteColor];

                [_mainView addSubview:timeView];

                

                UILabel * tLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 0, 60, 60)];

                tLabel.text = @"剩余";

                tLabel.textColor = [UIColor blackColor];

                tLabel.font = [UIFont systemFontOfSize:20];

                [timeView addSubview:tLabel];

                

                UILabel * mLabel = [[UILabel alloc]initWithFrame:CGRectMake(35, 30, 100, 100)];

                mLabel.text = strTime;

                mLabel.font = [UIFont systemFontOfSize:80];

                mLabel.textColor = [UIColor redColor];

                [timeView addSubview:mLabel];

                

                UILabel * lLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 100, 60, 60)];

                lLabel.text = @"分钟";

                lLabel.textColor = [UIColor redColor];

                lLabel.font = [UIFont systemFontOfSize:20];

                [timeView addSubview:lLabel];

            });

            timeout--;

            

        }  

    });  

    dispatch_resume(_timer);

 

#pragrm mark ---NSTimer---

  1. int secondsCountDown; //倒计时总时长  
  2. NSTimer *countDownTimer;  
  3. UILabel *labelText;  

 

 

 

  1. //创建UILabel 添加到当前view  
  2. labelText=[[UILabel alloc]initWithFrame:CGRectMake(10, 120, 120, 36)];  
  3. [self.view addSubview:labelText];  
  4.   
  5. //设置倒计时总时长  
  6. secondsCountDown = 60;//60秒倒计时  
  7. //开始倒计时  
  8. countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 timeFireMethod  
  9.   
  10. //设置倒计时显示的时间  
  11. labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];  

 

 

  1. -(void)timeFireMethod{  
  2.     //倒计时-1  
  3.     secondsCountDown--;  
  4.     //修改倒计时标签现实内容  
  5.     labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];  
  6.     //当倒计时到0时,做需要的操作,比如验证码过期不能提交  
  7.     if(secondsCountDown==0){  
  8.         [countDownTimer invalidate];  
  9.         [labelText removeFromSuperview];  
  10.     }  
  11. }  

 

转载于:https://www.cnblogs.com/vikki0620/p/5545365.html

相关文章:

  • iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
  • 如何设置电脑的固定IP地址
  • 优质博士的养成之道——对话2015微软学者奖学金获得者
  • 小凡带你搭建本地的光盘yum源
  • Retrofit get post query filed FiledMap
  • ActiveMQ集群应用
  • 拉格朗日插值法
  • 在Mysql中如何显示所有用户?
  • 第十五周 6.6 --- 6.12
  • 主键外键练习
  • 最适合初学者的语言是什么?
  • mybatis+springmvc+jbpm4整合配置
  • 企业集群平台架构实现与应用实战
  • 人月神话阅读笔记—第四章
  • 数据库复习①
  • __proto__ 和 prototype的关系
  • 【RocksDB】TransactionDB源码分析
  • 78. Subsets
  • ES6语法详解(一)
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • gulp 教程
  • PAT A1092
  • PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql
  • Sass Day-01
  • Spark RDD学习: aggregate函数
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 记一次删除Git记录中的大文件的过程
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • No resource identifier found for attribute,RxJava之zip操作符
  • 【干货分享】dos命令大全
  • 2017年360最后一道编程题
  • 7行Python代码的人脸识别
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • (13):Silverlight 2 数据与通信之WebRequest
  • (BFS)hdoj2377-Bus Pass
  • (C语言)fgets与fputs函数详解
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (四)鸿鹄云架构一服务注册中心
  • (转) Android中ViewStub组件使用
  • (转)平衡树
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • .net mvc部分视图
  • .NET 设计模式初探
  • .netcore 获取appsettings
  • .NET学习教程二——.net基础定义+VS常用设置
  • :not(:first-child)和:not(:last-child)的用法
  • [ 第一章] JavaScript 简史
  • [BZOJ 3531][Sdoi2014]旅行(树链剖分+线段树)
  • [BZOJ]4817: [Sdoi2017]树点涂色
  • [codeforces]Recover the String
  • [COGS 622] [NOIP2011] 玛雅游戏 模拟
  • [Electron] 将应用打包成供Ubuntu、Debian平台下安装的deb包
  • [excel与dict] python 读取excel内容并放入字典、将字典内容写入 excel文件
  • [Flex][问题笔记]TextArea滚动条问题