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
原创粉丝点击