2017-10-21 82 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秒。你的代码在同一时间之后调用所有的操作。 –