iOS开发示例————短信验证码倒计时
来源:互联网 发布:死亡笔记 知乎 编辑:程序博客网 时间:2024/06/03 18:29
这是我进公司以来算式第一个正式demo的编写,磨了三天,求助很多朋友,真是不容易啊,写完之后发现思路可以更好,望多多指教。
功能需求:点击button,开始倒计时显示,通过导航控制器pop到上一个界面逛一会儿,再回来倒计时显示继续,之间不间断,到00停止。
逻辑过程:利用NSTimer定时器实现倒计时功能,当界面pop回上一个界面时,NSTimer被销毁,再push进来时通过判断button之前是否被点击过来创建NSTimer继续倒计时。
要计算上一次pop出去——>push进来的间隔时间:interval,这样可以计算出多次进出界面后在界面外的累计总时间(allInterval)。interval = push进来的当前时间(inDate) - pop出去的当前时间outDate(也需要本地化存储)。
要计算pop出去——>push进来的间隔总时间:allInterval,设置为静态类型,下次重新开启app时此值可以被置空。
还要计算上次在当前界面倒计时的运行时间:runtime,设置为静态类型,并将此数据本地化,以便下次进来界面显示更新时间时便于调用来计算。在后面的判断当中runtime > 0则要计算进来的更新显示时间(i-- - allInterval - runtime),如果runtime == 0 则要设置正常倒计时显示(i--)。
设置判断button是否被点击BOOL类型为静态类型,此时默认为NO。在后面的判断中,如果是被点击的状态则要创建定时器,并开启。
实现:
在实现界面的.h文件中定义三个静态类型的对象
#import <UIKit/UIKit.h>static BOOL buttonSelected;static NSInteger runTime;static NSInteger allInterval;@interface DemoCountDownLabelViewController : UIViewController@end在实现界面的.m文件中实现具体逻辑功能
#import "DemoCountDownLabelViewController.h"#define kScreenWidth [UIScreen mainScreen].bounds.size.width@interface DemoCountDownLabelViewController () { NSInteger i; UILabel *timeLabel; UIButton *beginButton; NSDate *outDate; NSDate *inDate; NSInteger interval; NSTimer *timer;}@end@implementation DemoCountDownLabelViewController- (void)viewDidLoad { [super viewDidLoad]; i = 60; beginButton = [UIButton buttonWithType:UIButtonTypeCustom]; beginButton.frame = CGRectMake(kScreenWidth / 2 - 50, 100, 100, 50); beginButton.backgroundColor = [UIColor orangeColor]; [beginButton setTitle:@"开始倒计时" forState:UIControlStateNormal]; [beginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [beginButton addTarget:self action:@selector(beginButtonAction) forControlEvents:UIControlEventTouchUpInside]; timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 50, 200, 100, 60)]; timeLabel.textAlignment = NSTextAlignmentCenter; timeLabel.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:beginButton]; [self.view addSubview:timeLabel]; //获取进入时间 inDate = [NSDate date]; NSLog(@"进入界面当前时间:%@", inDate); //取出本地化跳出界面时间 NSUserDefaults *outTime = [NSUserDefaults standardUserDefaults]; outDate = [outTime objectForKey:@"lastTime"]; NSLog(@"之前跳出界面的时间:%@", outDate); if (buttonSelected) { //计算界面外运行时间 NSTimeInterval date1 = [inDate timeIntervalSinceReferenceDate]; NSTimeInterval date2 = [outDate timeIntervalSinceReferenceDate]; interval = date1 - date2; allInterval += interval; NSLog(@"在外总时间:%li", allInterval); timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; }}- (void)beginButtonAction { timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; buttonSelected = YES; [[NSRunLoop currentRunLoop] run];}- (void)timerAction:(NSTimer *)Timer { if (runTime > 0) { timeLabel.text = [NSString stringWithFormat:@"还剩%02li秒", (i-- - runTime - allInterval)]; if ((i - runTime - allInterval) < 0) { timeLabel.text = [NSString stringWithFormat:@"还剩00秒"]; [timer invalidate]; beginButton.enabled = NO; } beginButton.enabled = NO; } else if (runTime == 0) { timeLabel.text = [NSString stringWithFormat:@"还剩%02li秒",i--]; if (i < 0) { timeLabel.text = [NSString stringWithFormat:@"还剩00秒"]; [timer invalidate]; } beginButton.enabled = NO; }}- (void)viewWillDisappear:(BOOL)animated { [timer invalidate]; timer = nil; //本地化离开界面的当前时间 outDate = [NSDate date]; NSUserDefaults *outTime = [NSUserDefaults standardUserDefaults]; [outTime setObject:outDate forKey:@"lastTime"]; //本地化当前界面运行时间 runTime += 60 - i; NSUserDefaults *runTimeValue = [NSUserDefaults standardUserDefaults]; [runTimeValue setInteger:runTime forKey:@"runTimeKey"];}
0 0
- iOS开发示例————短信验证码倒计时
- iOS开发——短信验证码
- JAVA学习代码——短信验证码倒计时功能
- JAVA学习代码——短信验证码倒计时功能
- iOS开发——App集成短信验证码
- iOS开发——App集成短信验证码
- iOS开发-关于短信验证码倒计时
- iOS 倒计时功能(短信验证码)
- IOS短信验证码倒计时添加
- iOS发送短信后验证码倒计时
- iOS 之获取短信验证码倒计时
- iOS开发问题——简单的验证码倒计时显示
- iOS常用技术 —获取验证码 倒计时 实现
- 短信验证码倒计时
- 短信验证码倒计时
- 短信验证码倒计时
- iOS开发中获取短信验证码倒计时按钮的实现
- IOS开发笔记——实现倒计时
- iOS——视频点播、视频直播、飞心点赞、弹幕
- 经纬度坐标转为上海地方坐标代码(js代码)
- 关于k3中自定义字段的数据计算精度的问题
- ubuntu安装python集成环境Anaconda,并搭建tensorflow学习框架
- JNI官方文档翻译1-开篇
- iOS开发示例————短信验证码倒计时
- jQuery操作select下拉框的text值和value值的方法
- [bzoj1603]: [Usaco2008 Oct]打谷机 搜索
- scramble-string
- SHA256兼容性
- Web Service学习与初步认识
- theano学习指南3(翻译)-多层感知器模型
- 我喜欢微服务,函数式编程的原因
- 戴尔PowerEdge R730 机架式服务器安装 Ubuntu 14.04.3 LTS