2017-10-21 70 views
-1

我有NSTimer動作它。但是我想在第一次5秒之後,第二次4秒之後打電話行動 ...。非相等scheduledTimerWithTimeInterval NSTimer Objective-C

_timepast = 6; 
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleDelay) userInfo:nil repeats:YES]; 


-(void)handleDelay 
{ 
    _timepast = _timepast - 1; 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_timepast * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
     [self handleTimer]; 
    }); 
} 
+0

因此,這意味着你要反轉定時器,對不對? –

+0

@HardikShah是的,沒錯。 –

回答

0

的一切,如果你想5秒後開始行動,然後創建後5秒的延時,觸發第一個計時器,然後無效第一計時器並開始用5 countDown方法第二定時器的裝置5秒後的第一個延遲你的選擇器調用並處理其中的動作。我給你代碼如何秒計時器的工作原理。我希望你能知道如何處理第一個定時器5秒的延遲。

  1. 創建2個可變

    var timerCount = 5

    創建計時器的var timer : Timer!

  2. 1秒的時間間隔

    timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(self.startTimer), userInfo: nil, repeats: true)

  3. 在選擇方法寫反向檢查一樣,

    func startTimer() { if timerCount > 0 { label.text = String(timerCount--) } else { timer.invalidate() } }

+0

它不起作用..我想要5秒後我的第一個動作呼叫,4後的下一個動作,接下來的3和下一個動作之後.5秒。你的代碼在同一時間之後調用所有的操作。 –

相關問題