//类变量
int seconds;
NSTimer *timer;
//启动
-(void)startTimer
{
seconds = 86400*2+3600+60+24; //2天1时1分24秒
if (model.sytime.intValue>0) {
if (timer == nil) {
seconds =model.sytime.intValue;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerAction:) userInfo:nil repeats:true];
}
}
}
//结束
-(void)closeTimer{
if (timer != nil) {
[timer invalidate];
}
}
//定时执行
-(void) timerAction:(NSTimer*)theTimer
{
_sytimeLabel.text =[self timeFormatted:seconds];
seconds--;
if (seconds == 0) {
[self closeTimer];
}
//打印当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *date = [NSDate date] ;
NSLog(@"%@",[formatter stringFromDate:date]);
}
//秒 转天:时:分:秒
- (NSString *)timeFormatted:(int)totalSeconds
{
int tseconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = (totalSeconds / 3600) % 24;
int days = (totalSeconds / 3600)/24;
return [NSString stringWithFormat:@"%02d天%02d小时%02d分%02d秒",days,hours, minutes,tseconds];
}
Post Views: 678