Swift 中 for 循环中的计时器
Timer in the for loop in Swift
我想 运行 循环内的计时器仅用于给定的循环次数。还有一个外环。
用户将输入他想要的重复次数、每次重复的计数、两次计数之间的时间间隔。
应用程序将显示每次重复的次数。完成所有重复后,应用程序将停止计时器。
但我觉得很难。看起来计时器忽略了 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)
}
}
我认为你的计时器应该不在循环中了。
例如:
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()
}
在不知道你在问什么的情况下,你可以在循环中设置。更多详细信息肯定会有所帮助。
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()
}
}
需要在计时器处理程序中管理重复计数。
您通常将计时器作为一个实例 属性。 (您可能需要使计时器无效,例如,当 viewWillDisappear(_:)
时。)
因此,您可能需要在 class:
中写下这样的内容
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
}
}
使用与class 相同的方法从某处启动计时器:
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)
我想 运行 循环内的计时器仅用于给定的循环次数。还有一个外环。
用户将输入他想要的重复次数、每次重复的计数、两次计数之间的时间间隔。
应用程序将显示每次重复的次数。完成所有重复后,应用程序将停止计时器。
但我觉得很难。看起来计时器忽略了 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)
}
}
我认为你的计时器应该不在循环中了。
例如:
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()
}
在不知道你在问什么的情况下,你可以在循环中设置。更多详细信息肯定会有所帮助。
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()
}
}
需要在计时器处理程序中管理重复计数。
您通常将计时器作为一个实例 属性。 (您可能需要使计时器无效,例如,当 viewWillDisappear(_:)
时。)
因此,您可能需要在 class:
中写下这样的内容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
}
}
使用与class 相同的方法从某处启动计时器:
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)