iOS實(shí)現(xiàn)倒計(jì)時(shí)的三種方式
做iOS app開(kāi)發(fā)的過(guò)程當(dāng)中,經(jīng)常會(huì)出現(xiàn)獲取驗(yàn)證碼等需求,這個(gè)時(shí)候一般會(huì)使用倒計(jì)時(shí)來(lái)提示用戶,本文主要總結(jié)iOS開(kāi)發(fā)中常用的三種倒計(jì)時(shí)的實(shí)現(xiàn)方式。
Thread方式實(shí)現(xiàn)
NSTimer方式實(shí)現(xiàn) (此種方式實(shí)現(xiàn)的時(shí)候,要注意Timer invalidate的時(shí)機(jī),防止循環(huán)引用)
GCD方式實(shí)現(xiàn)
直接上代碼吧
#define TIMECOUNT 60
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIButton *firstBtn;
@property (strong, nonatomic) IBOutlet UIButton *secondBtn;
@property (strong, nonatomic) IBOutlet UIButton *thirdBtn;
@property (assign, nonatomic) int count;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.count = TIMECOUNT;
}
- (void)viewDidDisappear:(BOOL)animated {
//頁(yè)面消失的時(shí)候暫停定時(shí)器,防止出現(xiàn)循環(huán)引用,導(dǎo)致內(nèi)存泄漏
[self.timer invalidate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//------------***第一種方法***------------
#pragma mark 線程Thread方式實(shí)現(xiàn)
- (IBAction)firstBtnAction:(id)sender {
//創(chuàng)建一個(gè)后臺(tái)線程執(zhí)行計(jì)時(shí)操作
[self performSelectorInBackground:@selector(timerThread) withObject:nil];
}
- (void)timerThread {
for (int i = TIMECOUNT; i >= 0 ; i--) {
self.count--;
//切換到主線程中更新UI
[self performSelectorOnMainThread:@selector(updateFirstBtn) withObject:nil waitUntilDone:YES];
sleep(1);
}
}
//更新UI
- (void)updateFirstBtn {
NSString *str = nil;
if (self.count == 0) {
str = [NSString stringWithFormat:@"點(diǎn)擊獲取驗(yàn)證碼"];
self.firstBtn.userInteractionEnabled = YES;
} else {
str = [NSString stringWithFormat:@"%d秒后重新獲取",self.count];
self.firstBtn.userInteractionEnabled = NO;
}
[self.firstBtn setTitle:str forState:UIControlStateNormal];
}
//------------***第二種方法***------------
#pragma mark NSTimer實(shí)現(xiàn)
- (IBAction)secondBtnAction:(id)sender {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
}
//定時(shí)操作,更新UI
- (void)handleTimer {
if (self.count == 0) {
self.secondBtn.userInteractionEnabled = YES;
[self.secondBtn setTitle:[NSString stringWithFormat:@"點(diǎn)擊獲取驗(yàn)證碼"] forState:UIControlStateNormal];
self.count = TIMECOUNT;
[self.timer invalidate];
} else {
self.secondBtn.userInteractionEnabled = NO;
[self.secondBtn setTitle:[NSString stringWithFormat:@"%d秒后重新獲取",self.count] forState:UIControlStateNormal];
}
self.count--;
}
//------------***第三種方法***------------
/**
* 1、獲取或者創(chuàng)建一個(gè)隊(duì)列,一般情況下獲取一個(gè)全局的隊(duì)列
* 2、創(chuàng)建一個(gè)定時(shí)器模式的事件源
* 3、設(shè)置定時(shí)器的響應(yīng)間隔
* 4、設(shè)置定時(shí)器事件源的響應(yīng)回調(diào),當(dāng)定時(shí)事件發(fā)生時(shí),執(zhí)行此回調(diào)
* 5、啟動(dòng)定時(shí)器事件
* 6、取消定時(shí)器dispatch源,【必須】
*
*/
#pragma mark GCD實(shí)現(xiàn)
- (IBAction)thirdBtnAction:(id)sender {
__block NSInteger second = TIMECOUNT;
//(1)
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//(2)
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
//(3)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//(4)
dispatch_source_set_event_handler(timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (second == 0) {
self.thirdBtn.userInteractionEnabled = YES;
[self.thirdBtn setTitle:[NSString stringWithFormat:@"點(diǎn)擊獲取驗(yàn)證碼"] forState:UIControlStateNormal];
second = TIMECOUNT;
//(6)
dispatch_cancel(timer);
} else {
self.thirdBtn.userInteractionEnabled = NO;
[self.thirdBtn setTitle:[NSString stringWithFormat:@"%ld秒后重新獲取",second] forState:UIControlStateNormal];
second--;
}
});
});
//(5)
dispatch_resume(timer);
}
@end