码迷,mamicode.com
首页 > 其他好文 > 详细

GCD 和Timer

时间:2016-05-31 12:02:11      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

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. }  

 

GCD 和Timer

标签:

原文地址:http://www.cnblogs.com/vikki0620/p/5545365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!