倒计时

作者: 春暖花已开 | 来源:发表于2018-11-25 22:22 被阅读13次

更新日志:2018-11-20 修复没有点击,退到后台,重新进入前台,触发倒计时的问题。

#import <UIKit/UIKit.h>

@interface SMTCountButton : UIButton

// 开始倒计时
- (void)timeDown;

// button重置为初始状态
- (void)resetButtonStatus;

@end
#import "SMTCountButton.h"

#import "NSTimer+SmartUtils.h"

@interface SMTCountButton ()

@property (nonatomic, strong) NSDate *endDate;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger wholeSeconds;

@property (nonatomic, assign) BOOL hasClicked;

@end

@implementation SMTCountButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self configure];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self configure];
    }
    return self;
}


#pragma mark - Public Methods

- (void)timeDown {
    
    self.hasClicked = YES;
    [self resetWholeSeconds];
    [self.timer sm_resume];
}


#pragma mark - Private Methods

- (void)configure {
    
    [self resetWholeSeconds];
    self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerCountDown) userInfo:nil repeats:YES useProxy:YES runloopMode:NSRunLoopCommonModes];
    [self.timer sm_pause];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActiveSelector) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActiveSelector) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)applicationWillResignActiveSelector {
    
    [self.timer sm_pause];
    self.endDate = [NSDate dateWithTimeIntervalSinceNow:self.wholeSeconds];
}

- (void)applicationDidBecomeActiveSelector {
    
    if (self.hasClicked) {
        self.wholeSeconds = (NSInteger)[self.endDate timeIntervalSinceDate:[NSDate date]];
        [self.timer sm_resume];
    }
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)timerCountDown {
    
    if (self.wholeSeconds > 0) {
        [self setTitle:[NSString stringWithFormat:@"%ld秒后重试", (long)self.wholeSeconds] forState:UIControlStateNormal];
        self.enabled = NO;
        self.wholeSeconds--;
    } else {
        [self resetButtonStatus];
    }
}

- (void)resetWholeSeconds {
    self.wholeSeconds = 60;
}

- (void)resetButtonStatus {
    self.hasClicked = NO;
    [self.timer sm_pause];
    [self resetWholeSeconds];
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.enabled = YES;
}

@end

题外话

来打破NSTimer的引用循环:

#import "NSTimer+MZExtension.h"

@implementation NSTimer (MZExtension)

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti block:(void(^)(void))block repeats:(BOOL)yesOrNo {
    
    return [NSTimer timerWithTimeInterval:ti target:self selector:@selector(blockInvoke:) userInfo:[block copy] repeats:yesOrNo];
}

+ (void)blockInvoke:(NSTimer *)timer {
    
    void(^block)(void) = timer.userInfo;
    !block ?: block();
}

@end

相关文章

  • 倒计时

    新年倒计时…中考倒计时…高考倒计时…告别学生倒计时…假期倒计时…睡觉倒计时…起床倒计时… 生活中,很多与我们息息相...

  • 倒计时不仅使我们焦躁,还可以使我们振奋啊

    中考倒计时,高考倒计时, 倒计时100天 倒计时50天 倒计时10天 倒计时5天 ........ 这个场景想必在...

  • 倒计时

    文/木子 开学倒计时了 爸妈回去倒计时了 教师节倒计时了 国庆节倒计时了 小儿12岁倒计时了 寒假倒计时了 元旦倒...

  • wordlist 13

    倒计时倒计时

  • wordlist 11

    倒计时倒计时

  • van-count-down 倒计时相关问题

    1. 倒计时重置问题 背景:业务需要在开启倒计时、暂停、恢复倒计时实时判断是否展示倒计时,并重置倒计时 问题:直接...

  • iOS开发-倒计时

    倒计时 倒计时60s 倒计时HH-MM-SS 1.倒计时60s 很多时候在点击按钮发送短信的时候需要倒计时读秒 2...

  • 10.27

    倒计时第一天,倒计时7,好想赶快到倒计时1。

  • 1小时倒计时

    倒计时 目前我们内部用的比较多的倒计时:一个小时的倒计时、12个小时的倒计时。 examinePassTime:审...

  • js中date倒计时

    获取倒计时之前,我们不妨先来看怎样获取当前时间吧!!! 获取倒计时 1、实现倒计时功能2、倒计时结束按钮可被点击,...

网友评论

    本文标题:倒计时

    本文链接:https://www.haomeiwen.com/subject/cyimqqtx.html