概述
簡(jiǎn)單的屬性設(shè)置就能擁有一個(gè)不怕【退出頁面/殺掉APP/修改系統(tǒng)時(shí)間】的倒計(jì)時(shí)按鈕
Code
使用
快速集成
pod 'AZTools'
添加頭文件
#import <UIButton+AZCountDown.h>
設(shè)置屬性
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 恢復(fù)倒計(jì)時(shí)
[self.button az_cd_recover];
}
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.button.az_cd_identify = @"AZCountDownDemoBtn";
self.button.az_cd_count = 60;
self.button.az_cd_countdownBlock = ^(NSInteger countDown, UIButton * _Nonnull button) {
[button setTitle:[NSString stringWithFormat:@"%lds",(long)countDown] forState:UIControlStateDisabled];
};
self.button.az_cd_endBlock = ^{
NSLog(@"倒計(jì)時(shí)結(jié)束!");
};
}
- (IBAction)startCountDown:(UIButton *)sender {
// 用戶點(diǎn)擊開始倒計(jì)時(shí)
[sender az_cd_start];
}
方案概述
防止通過退出頁面/殺掉APP的方式重置倒計(jì)時(shí)
通過唯一標(biāo)志位將倒計(jì)時(shí)的到期時(shí)間記錄在本地缆瓣,每次恢復(fù)az_cd_recover
倒計(jì)時(shí)的時(shí)候從本地讀取到期時(shí)間并計(jì)算剩余時(shí)間
防止修改系統(tǒng)時(shí)間修改倒計(jì)時(shí)
存儲(chǔ)的到期時(shí)間是服務(wù)器時(shí)間领追,和手機(jī)本地時(shí)間無關(guān)。這里主要用到了AZServiceDate
這個(gè)類序愚,想要達(dá)到完全和服務(wù)器時(shí)間同步還需在合適的時(shí)機(jī)調(diào)用AZServiceDate
的更新服務(wù)器時(shí)間方法漂坏。
// AZServiceDate.h
/** 以特定URL返回的Date為基準(zhǔn)更新ServiceDate */
+ (void)updateDateWithUrl:(nullable NSString *)url success:(dispatch_block_t)success failure:(dispatch_block_t)failure;
/** 通過 RFC3339 或者 RFC822 string 直接更新基準(zhǔn)時(shí)間 */
+ (void)updateStandardNetResponseDate:(nullable NSString *)dateStr;