我有3个时间:

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,但另一种方法是-不。

最佳答案

我找到了解决办法:
您可以删除.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)

关于swift - NSTimers swift 不开火,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27855291/

10-10 01:39