iOS开发 显示倒计时时间
2016-01-15 本文已影响6884人
安然slience
- (void)viewDidLoad {
secondsCountDown = 172800;//倒计时秒数
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 countDownAction
//设置倒计时显示的时间
NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];//时
NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];//分
NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];//秒
NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
NSLog(@"time:%@",format_time);
self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bigImageView.frame.size.width, bigImageView.frame.size.height)];
self.timeLabel.text = [NSString stringWithFormat:@"倒计时 %@",format_time];
self.timeLabel.textAlignment = NSTextAlignmentCenter;
self.timeLabel.font = FONT_19;
self.timeLabel.textColor = [UIColor redColor];
[self.View addSubview:self.timeLabel];
}
-(void) countDownAction{
//倒计时-1
secondsCountDown--;
NSString *str_hour = [NSString stringWithFormat:@"%02ld",secondsCountDown/3600];
NSString *str_minute = [NSString stringWithFormat:@"%02ld",(secondsCountDown%3600)/60];
NSString *str_second = [NSString stringWithFormat:@"%02ld",secondsCountDown%60];
NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
//修改倒计时标签现实内容
self.timeLabel.text=[NSString stringWithFormat:@"倒计时 %@",format_time];
//当倒计时到0时,做需要的操作,比如验证码过期不能提交
if(secondsCountDown==0){
[countDownTimer invalidate];
}
}