- (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];
}
}
iOS开发 显示倒计时时间
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
- 半夜醒来,读了两遍三毛写的《说给自己听》,我深深地叹服她内心的安静,从容,与智慧。 人们说,漂亮的皮囊千篇一律,有...