我有3个NSTimers:
var timer = NSTimer()
var scoreTimer = NSTimer()
var gameTmer = NSTimer()
然后在“viewDidLoad”中:
var sel = Selector("testFunc:")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: sel, userInfo: nil, repeats: true)
scoreTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: sel, userInfo: nil, repeats: true)
gameTmer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: sel, userInfo: nil, repeats: true)
testFunc就是这样:
func testFunc(timer : NSTimer){
println("working")
}
但他们不起作用。如果我尝试使用“fire()”,那么他们会调用testFunc,但其他方式 - 不。
答案 0 :(得分:1)
我找到了解决方案:
您可以删除.scheduledTimerWithTimeInterval
并为每个计时器添加NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
。所以,你的代码看起来像这样:
var sel = Selector("testFunc:")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: sel, userInfo: nil, repeats: true)
scoreTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: sel, userInfo: nil, repeats: true)
gameTmer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: sel, userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
NSRunLoop.mainRunLoop().addTimer(scoreTimer, forMode: NSRunLoopCommonModes)
NSRunLoop.mainRunLoop().addTimer(gameTmer, forMode: NSRunLoopCommonModes)