美文网首页
iOS开发 倒计时

iOS开发 倒计时

作者: 红先生的小简书 | 来源:发表于2017-11-08 13:54 被阅读0次
@interface ViewController ()
{
    NSInteger timeCount;//秒数
}
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, copy) NSTimer *timer;
@end
timeCount = 300;//倒计时秒数

// NSString *hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];//时    
NSString *minute = [NSString stringWithFormat:@"%02ld",(timeCount%3600)/60];//分
NSString *second = [NSString stringWithFormat:@"%02ld",timeCount%60];//秒

_label.text = [NSString stringWithFormat:@"%@:%@",minute,second];
    
_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
- (void)countDownAction {
    
    timeCount--;
    NSLog(@"%ld",(long)timeCount);
    NSString *minute = [NSString stringWithFormat:@"%02ld",(timeCount%3600)/60];//分
    NSString *second = [NSString stringWithFormat:@"%02ld",timeCount%60];//秒
    
    _label.text = [NSString stringWithFormat:@"%@:%@",minute,second];
    if (timeCount == 0) {
        [_timer invalidate];
    }
}

相关文章

网友评论

      本文标题:iOS开发 倒计时

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