iOS开发 倒计时

@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];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容