在Objective-C中为scheduledTimerWithTimeInterval反转NSTimer

时间:2021-08-19 20:37:57

I have NSTimer and Action for it. But I want to call Action after 5s for first time, 4s for second time and ... .

我有NSTimer和Action。但我想第一次在5s之后调用Action,第二次调用4s并且....

_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];
   });
}

1 个解决方案

#1


1  

First of all if you want to start action after 5 sec and then create first timer which trigger after 5 sec delay and then invalidate first timer and start your second timer with countDown of 5. It means after 5 sec delay you will your selector called and handle action in it. I am giving you code for how second timer works. I hope you will know how to handle first timer for 5 sec delay.

首先,如果你想在5秒后开始动作,然后创建第一个定时器,它在5秒延迟后触发,然后使第一个定时器无效,并以countDown为5启动你的第二个定时器。这意味着在5秒延迟后你将选择器调用处理它的行动。我给你代码第二个计时器如何工作。我希望你知道如何处理第一个计时器5秒延迟。

  1. Create 2 variable

    创建2个变量

    var timerCount = 5

    var timerCount = 5

    var timer : Timer!

    var timer:计时器!

  2. Create timer of 1 sec interval

    创建1秒间隔的计时器

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

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

  3. In the selector method write reverse checking like,

    在选择器方法中写反向检查,如,

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

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

#1


1  

First of all if you want to start action after 5 sec and then create first timer which trigger after 5 sec delay and then invalidate first timer and start your second timer with countDown of 5. It means after 5 sec delay you will your selector called and handle action in it. I am giving you code for how second timer works. I hope you will know how to handle first timer for 5 sec delay.

首先,如果你想在5秒后开始动作,然后创建第一个定时器,它在5秒延迟后触发,然后使第一个定时器无效,并以countDown为5启动你的第二个定时器。这意味着在5秒延迟后你将选择器调用处理它的行动。我给你代码第二个计时器如何工作。我希望你知道如何处理第一个计时器5秒延迟。

  1. Create 2 variable

    创建2个变量

    var timerCount = 5

    var timerCount = 5

    var timer : Timer!

    var timer:计时器!

  2. Create timer of 1 sec interval

    创建1秒间隔的计时器

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

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

  3. In the selector method write reverse checking like,

    在选择器方法中写反向检查,如,

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

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

相关文章