Ios Swift中for循环中的计时器
我只想在一个循环中为给定数量的循环运行计时器。还有一个外环 用户将输入他想要的重复次数,每次重复的次数,两次重复之间的时间间隔 应用程序将显示每次重复的次数。当所有重复完成后,应用程序将停止计时器 但我发现这很难。它看起来好像计时器忽略了for循环,而它本身就是一个循环,当我们只发出timer.invalidate()时它就会停止 有什么想法吗Ios Swift中for循环中的计时器,ios,swift,nstimer,Ios,Swift,Nstimer,我只想在一个循环中为给定数量的循环运行计时器。还有一个外环 用户将输入他想要的重复次数,每次重复的次数,两次重复之间的时间间隔 应用程序将显示每次重复的次数。当所有重复完成后,应用程序将停止计时器 但我发现这很难。它看起来好像计时器忽略了for循环,而它本身就是一个循环,当我们只发出timer.invalidate()时它就会停止 有什么想法吗 for x in 0...HowManyRepetitions { counter = 0
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()
}
我想你的计时器应该出环了 例如:
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()
}
}
如果你不知道自己在问什么,你就可以进入循环。更多的细节肯定会有所帮助
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()
}
}
重复计数需要在计时器处理程序中进行管理 您通常将计时器作为实例属性。(例如,当
视图将消失(:)
时,您可能需要使计时器无效)
因此,你可能需要在课堂上写一些类似的东西:
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)
重复计数需要在计时器处理程序中进行管理 您通常将计时器作为实例属性。(例如,当
视图将消失(:)
时,您可能需要使计时器无效)
因此,你可能需要在课堂上写一些类似的东西:
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)
你的计时器实际上什么都不做,因为它有一个nil选择器。为什么你有计时器?你的计时器实际上没有做任何事情,因为它有一个零选择器。你为什么有计时器?