iOS —定時(shí)器的使用

一、NSTimer的使用

- (void)createNSTimer
{
    // 調(diào)用創(chuàng)建方法后,target對(duì)象的計(jì)數(shù)器會(huì)加1,直到執(zhí)行完畢,自動(dòng)減1。如果是循環(huán)執(zhí)行的話,就必須手動(dòng)關(guān)閉,否則可以不執(zhí)行釋放方法
    // 必須進(jìn)行停止 —— 釋放 [_timer invalidate];
    // 自動(dòng)把timer加入MainRunloop的NSDefaultRunLoopMode中
#if 0
    _GZDTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(Logs) userInfo:nil repeats:YES];
#endif
    // 這種方法需要手動(dòng)添加到NSDefaultRunLoopMode runloop的運(yùn)行循環(huán)中,否則無(wú)法運(yùn)行
    _GZDTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(Logs01) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_GZDTimer forMode:NSDefaultRunLoopMode];
}
- (void)Logs
{
    NSLog(@"NSTimer 自動(dòng)運(yùn)行循環(huán)  >>>>>>>");
}
- (void)Logs01
{
    NSLog(@"NSTimer 手動(dòng)運(yùn)行循環(huán)  >>>>>>>");
}

二、CADisplayLink的使用

// 使用CADisplayLink需要記得停止定時(shí)器,停止的方法
    // 停止displayLink的定時(shí)器
    //[self.displayLink invalidate];
    //self.displayLink = nil;

- (void)createCADisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(GZDDisplayLink)];
    // NSInteger類型的值,用來(lái)設(shè)置間隔多少幀調(diào)用一次selector方法,默認(rèn)值是1,即每幀都調(diào)用一次。
    
    // readOnly的CFTimeInterval值,表示兩次屏幕刷新之間的時(shí)間間隔。需要注意的是,該屬性在target的selector被首次調(diào)用以后才會(huì)被賦值。selector的調(diào)用間隔時(shí)間計(jì)算方式是:調(diào)用間隔時(shí)間 = duration × frameInterval。
    
    /**當(dāng)把CADisplayLink對(duì)象add到runloop中后,selector就能被周期性調(diào)用,類似于重復(fù)的NSTimer被啟動(dòng)了;執(zhí)行invalidate操作時(shí),CADisplayLink對(duì)象就會(huì)從runloop中移除,selector調(diào)用也隨即停止,類似于NSTimer的invalidate方法。**/
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)GZDDisplayLink
{
    NSLog(@"GZDDisplayLink >>>>>>> ");
}

三、GCD

- (void)createGCD
{
    /**
     *  只執(zhí)行一次
     */
#if 0
    double delayInSeconds = 2.0;//兩秒后,執(zhí)行一次
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            //執(zhí)行事件
        NSLog(@"只執(zhí)行一次>>>>>>>");
        });
#endif
    
    NSTimeInterval timerInterval = 1.0; //設(shè)置時(shí)間間隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // _timer必須為全局變量
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), timerInterval * NSEC_PER_SEC, 0); //每秒執(zhí)行
    dispatch_source_set_event_handler(_timer, ^{
        //在這里執(zhí)行事件
        NSLog(@"GCD -----  1s執(zhí)行一次  >>>>>>>");
    });
    dispatch_resume(_timer);
}

詳細(xì)的源碼可以去我的github下載參考
https://github.com/daniel1214/160829-TimeUser

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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