美文网首页关于iOSObject-C
iOS开发 显示倒计时时间

iOS开发 显示倒计时时间

作者: 安然slience | 来源:发表于2016-01-15 10:41 被阅读6884次
    - (void)viewDidLoad { 
        
        secondsCountDown = 172800;//倒计时秒数
        countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 countDownAction
        //设置倒计时显示的时间
        NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];//时
        NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];//分
        NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];//秒
        NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
        NSLog(@"time:%@",format_time);
    
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bigImageView.frame.size.width, bigImageView.frame.size.height)];
        self.timeLabel.text = [NSString stringWithFormat:@"倒计时   %@",format_time];
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.font = FONT_19;
        self.timeLabel.textColor = [UIColor  redColor];
        [self.View addSubview:self.timeLabel];
        }
    
    -(void) countDownAction{
        //倒计时-1
        secondsCountDown--;
        NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];
        NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];
        NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];
        NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
        //修改倒计时标签现实内容
        self.timeLabel.text=[NSString stringWithFormat:@"倒计时   %@",format_time];
        //当倒计时到0时,做需要的操作,比如验证码过期不能提交
        if(secondsCountDown==0){
            [countDownTimer invalidate];
        }
        }
    

    附上github地址:https://github.com/ZSyingyu/ZYYCountDownDemo.git

    相关文章

      网友评论

      • Dayu大鱼:设置的倒计时秒数不管用,直接全部为零 该怎么解决?
      • macfai:楼主,能给个完整的demo吗,或者github地址,多谢
      • 超_iOS:你好,我在model 中写了个倒计时是正常,但是要在VC中的view 显示,这个view的时间不跟着计时器刷新而总是显示最初的值,怎么办>?

      本文标题:iOS开发 显示倒计时时间

      本文链接:https://www.haomeiwen.com/subject/ytjhkttx.html