iOS 單例計(jì)時(shí)器(頁(yè)面切換仍然計(jì)時(shí))

iOS 單例計(jì)時(shí)器(頁(yè)面切換仍然計(jì)時(shí))?NSTimer 定時(shí)器(2)?GCD 多線程(4)?

//–––––––––––––––––––––單例.h––––––––––––––––––––––––

#import?

@interface?CaptchaTimerManager :NSObject

@property?(nonatomic,assign)__blockint?timeout;

+ (id)sharedTimerManager;

- (void)countDown;

@end

//–––––––––––––––––––––單例.m––––––––––––––––––––––––

#import"CaptchaTimerManager.h"

@implementation?CaptchaTimerManager

+ (id)sharedTimerManager{

?static?CaptchaTimerManager?*manager =nil;

?staticdispatch_once_t?onceToken;

?dispatch_once(&onceToken, ^{

?if?(manager ==nil) {

? ? ? ? ? ? manager = [[selfalloc]init];

? ? ? ? }

? ? });

?return?manager;

}

- (void)countDown{


?if?(_timeout?>0) {

?dispatch_queue_t?queue =?dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

?dispatch_source_t?_timer =?dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,?0,queue);

?dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0);?//每秒執(zhí)行

?dispatch_source_set_event_handler(_timer, ^{

?if(_timeout<=0){//倒計(jì)時(shí)結(jié)束,關(guān)閉

?dispatch_source_cancel(_timer);

? ? ? ? ? ? }else{

?_timeout--;

? ? ? ? ? ? }

? ? ? ? });

?dispatch_resume(_timer);

? ? }

}

@end

//–––––––––––––––––––––調(diào)用––––––––––––––––––––––––

#import?"CaptchaTimerManager.h"

@property?(weak,nonatomic)?IBOutletUIButton?*getNUmber;

@property?(nonatomic,assign)?int?timeout;

- (IBAction)getNumberButton:(UIButton?*)sender {

?_getNUmber.enabled?=NO;

?_timeout?=10;?//倒計(jì)時(shí)時(shí)間

? ? [selftimerCountDown];

}

-(void)viewWillAppear:(BOOL)animated{

?CaptchaTimerManager?*manager = [CaptchaTimerManagersharedTimerManager];

?int?temp = manager.timeout;

?if?(temp >0) {

?_timeout= temp;//倒計(jì)時(shí)時(shí)間

?_getNUmber.enabled?=NO;

? ? ? ? [selftimerCountDown];

? ? }else{

?_getNUmber.enabled?=YES;

? ? }

}

- (void)viewWillDisappear:(BOOL)animated{

? ? [superviewWillDisappear:animated];

?if?(self.timeout?>0) {

?CaptchaTimerManager?*manager = [CaptchaTimerManagersharedTimerManager];

?if?(manager.timeout?==0) {

? ? ? ? ? ? manager.timeout?=_timeout;

[manager?countDown];

? ? ? ? }

?_timeout?=?0;//置為0,釋放controller

? ? }

}

//控制器里的計(jì)時(shí)器

- (void)timerCountDown {

?dispatch_queue_t?queue =?dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

?dispatch_source_t?_timer =?dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,?0,queue);

?dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0);?//每秒執(zhí)行

?dispatch_source_set_event_handler(_timer, ^{

?if(_timeout<=0){//倒計(jì)時(shí)結(jié)束,關(guān)閉

? ? ? ? ? ?dispatch_source_cancel(_timer);

? ? ? ? ? ?dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ?//這里寫(xiě)倒計(jì)時(shí)結(jié)束button的處理

? ? ? ? ? ? });

? ? ? ? }else{

? ? ? ? ? ?dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ?//這里寫(xiě)倒計(jì)時(shí)期間button的處理(重設(shè)button的tiitle、用戶交互等)

?if?(_timeout==1) {

?self.title?=@"輸入手機(jī)號(hào)";

?_getNUmber.enabled?=YES;

}?else?{

?self.title?= [NSStringstringWithFormat:@"%d",_timeout];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? });

?_timeout--;

? ? ? ? }

? ? });

?dispatch_resume(_timer);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評(píng)論 19 139
  • 一、在后臺(tái)工作量少 當(dāng)用戶沒(méi)有主動(dòng)使用你的應(yīng)用程序時(shí),系統(tǒng)會(huì)將其轉(zhuǎn)換為背景狀態(tài)。該系統(tǒng)最終可能會(huì)暫停您的應(yīng)用程序,...
    弗利撒閱讀 977評(píng)論 1 0
  • 寫(xiě)項(xiàng)目的時(shí)候需要一個(gè)計(jì)時(shí)器來(lái)做短信驗(yàn)證碼的倒計(jì)時(shí),場(chǎng)景需求是按鈕點(diǎn)擊后開(kāi)始讀秒,按鈕不可用,當(dāng)讀秒結(jié)束按鈕可...
    lg_ss_long閱讀 207評(píng)論 0 0
  • 無(wú)善無(wú)惡心之體, 有善有惡意之動(dòng)。 知善知惡是良知, 為善去惡是格物。 -------陽(yáng)...
    吹泡泡的貓閱讀 839評(píng)論 0 0
  • 今天,我和女兒去看了一部電影,電影的名字是:舉起手來(lái),這是一部愛(ài)國(guó)電影,講述了日本鬼子,為了得到一尊佛像而費(fèi)盡心思...
    duhongjuan閱讀 159評(píng)論 0 0

友情鏈接更多精彩內(nèi)容