iOS开发 倒计时

2017-11-08  本文已影响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];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读