Swift中for循环中的计时器

时间:2016-08-28 04:48:24

标签: ios swift nstimer

我想在一个循环内运行定时器,仅用于给定数量的循环。还有一个外环。

用户将输入他想要的重复次数,每次重复的次数,两次计数之间的时间间隔。

app会显示每次重复的次数。当所有重复完成后,app将停止计时器。

但我觉得很困难。它看起来像timer忽略for循环,并且是一个循环本身,当我们只发出timer.invalidate()时会停止。

有什么想法吗?

for x in 0...HowManyRepetitions {

                counter = 0
                CountLabel.text = "\(counter)"
                RepetitionLabel.text = "\(x) / \(HowManyRepetitions)"

                for y in 0...HowManyCounts {

                    timer = NSTimer.scheduledTimerWithTimeInterval(PeriodBetween, target: self, selector: updateCounter, userInfo: nil, repeats: true)

                }

            }

3 个答案:

答案 0 :(得分:1)

我认为你的计时器应该不在循环中。

例如:

for x in 0...HowManyRepetitions {

                counter = 0
                CountLabel.text = "\(counter)"
                RepetitionLabel.text = "\(x) / \(HowManyRepetitions)"

                timer = NSTimer.scheduledTimerWithTimeInterval(PeriodBetween, target: self, selector: nil, userInfo: nil, repeats: true)

                for y in 0...HowManyCounts {
                   // doSomething
                    ...
                }

                timer.invalidate()

            }

答案 1 :(得分:1)

需要在计时器处理程序中管理重复计数。

您通常将计时器作为实例属性。 (您可能需要使计时器无效,例如,viewWillDisappear(_:)时。)

所以,你可能需要在你的课上写这样的东西:

var timer: NSTimer?
func startTimer(howManyCounts: Int, periodBetween: NSTimeInterval) {
    let userInfo: NSMutableDictionary = [
        "counter": 0,
        "howManyCounts": howManyCounts,
        "myName": "Timer"
    ]
    self.timer = NSTimer.scheduledTimerWithTimeInterval(periodBetween, target: self, selector: #selector(timerHandler), userInfo: userInfo, repeats: true)
}

@objc func timerHandler(timer: NSTimer) {
    guard let info = timer.userInfo as? NSMutableDictionary else {
        return
    }
    var counter = info["counter"] as? Int ?? 0
    let howManyCounts = info["howManyCounts"] as? Int ?? 0
    let myName = info["myName"] as? String ?? "Timer"
    counter += 1

    print("\(myName):\(counter)") //countLabel.text = "\(counter)"

    if counter >= howManyCounts {
        timer.invalidate()
    } else {
        info["counter"] = counter
    }
}

从同一类的方法中的某处启动计时器:

    startTimer(10, periodBetween: 3.0)

我不明白你为什么需要外循环,但如果你想让多个计时器工作,你需要保留所有计时器。

var timers: [NSTimer] = []
func startTimers(howManyRepetitions: Int, howManyCounts: Int, periodBetween: NSTimeInterval) {
    timers = []
    for x in 1...howManyRepetitions {
        let userInfo: NSMutableDictionary = [
            "counter": 0,
            "howManyCounts": howManyCounts,
            "myName": "Timer-\(x)"
        ]
        timers.append(NSTimer.scheduledTimerWithTimeInterval(periodBetween, target: self, selector: #selector(timerHandler), userInfo: userInfo, repeats: true))
    }
}

启动计时器:

    startTimers(3, howManyCounts: 4, periodBetween: 1.0)

答案 2 :(得分:0)

如果不确切地知道你在问什么,你可以在循环中设置。更多细节肯定会有所帮助。

let param = 0 //IN SCOPE

for y in 0...HowManyCounts {

     param++

     if param != HowManyCounts{ 
     timer = NSTimer.scheduledTimerWithTimeInterval(PeriodBetween, target: self, selector: nil, userInfo: nil, repeats: true)
     }else{
         timer.invalidate()
     }

}