iOS 实现倒计时显示 时 分 秒
来源:互联网 发布:知乎校园招聘2017 编辑:程序博客网 时间:2024/05/16 19:43
头部代码#import "YQHomeViewController.h"@interface YQHomeViewController ()//将sb中的label进行拖拽过来@property (weak, nonatomic) IBOutlet UILabel *timeLbl;//创建定时器(因为下面两个方法都使用,所以定时器拿出来设置为一个属性)@property(nonatomic,strong)NSTimer*countDownTimer;@end@implementation YQHomeViewController//倒计时总的秒数static NSInteger secondsCountDown = 86400;- (void)viewDidLoad { [super viewDidLoad];
实现步骤:
1.设置定时器(给定24小时)
一个定时器,定时器里面做设置间隔多少时间执行什么方法(countDownAction)
- (void)viewDidLoad { [super viewDidLoad]; //设置定时器 _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]; self.timeLbl.text = [NSString stringWithFormat:@"即将开始:%@",format_time]; //设置文字颜色 self.timeLbl.textColor = [UIColor blackColor ]; [self.view addSubview:_timeLbl];}
2.方法(countDownAction)(来自创建定时器时需要执行的方法)内部:总秒数递减
1.先递减
2.给时分秒字符串通过递减过后的秒数,重新计算数值,并输出显示
//实现倒计时动作-(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.timeLbl.text=[NSString stringWithFormat:@"即将开始:%@",format_time]; //当倒计时到0时做需要的操作,比如验证码过期不能提交 if(secondsCountDown==0){ [_countDownTimer invalidate]; } }
阅读全文
0 0
- iOS实现倒计时显示 时 分 秒
- iOS实现倒计时显示 时 分 秒
- iOS 实现倒计时显示 时 分 秒
- iOS倒计时的实现,显示剩余天、时、分、秒
- iOS倒计时,显示剩余天、时、分、秒
- iOS倒计时,显示剩余天、时、分、秒
- iOS倒计时,显示剩余天、时、分、秒
- 秒杀活动倒计时 倒计时的实现,显示剩余天、时、分、秒
- JS实现倒计时(时、分,秒)
- JS实现的倒计时(时、分、秒)
- JS实现倒计时(时、分,秒)
- JS 倒计时实现代码(时、分,秒)
- JS实现倒计时(天数,时,分,秒)
- JS实现倒计时(天、时、分,秒)
- 倒计时 多个同时支持 -- 显示*(时:分:秒)
- IOS 含天数、时、分、秒的倒计时
- 倒计时(时、分、秒)面板
- jQuery 倒计时 天、时、分、秒倒计时
- kafka中的topic为什么要进行分区?
- 网页特殊符号HTML代码大全
- python -- 基础知识测试
- 项目报错:Caused by: java.lang.ClassNotFoundException: Didn't find class "..."on path: DexPathList
- Git安装配置
- iOS 实现倒计时显示 时 分 秒
- springboot(八):RabbitMQ详解
- 第十一周 【项目2
- Java与JSON之间相互转换-1
- 异常分类
- 计算机网络体系结构和协议
- Axure导入元件库的两种方式-附完整元件库
- js调试工具Console命令详解
- Representative Sampling