美文网首页
一小时倒计时59:59

一小时倒计时59:59

作者: panda_熊猫 | 来源:发表于2018-02-02 17:13 被阅读50次
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    self.timeLabel.text = [NSString stringWithFormat:@"倒计时   %@",self.format_time];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.font = [UIFont systemFontOfSize:19];
    self.timeLabel.textColor = [UIColor  redColor];
    [self.view addSubview:self.timeLabel];
    
    
    __block NSInteger time = 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(time <= 0){ //倒计时结束,关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                NSString *str_minute = [NSString stringWithFormat:@"%02ld",(time%3600)/60];
                NSString *str_second = [NSString stringWithFormat:@"%02ld",time%60];
                self.format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
                self.timeLabel.text = [NSString stringWithFormat:@"倒计时   %@",self.format_time];
            });
            time--;
        }
    });
    dispatch_resume(_timer);

    相关文章

      网友评论

          本文标题:一小时倒计时59:59

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