NSTimer
在Swift中使用NSTimer来计时使用的是NSTimer中得静态方法scheduledTimerWithTimeInterval,函数的原型为
class func scheduledTimerWithTimeInterval(ti: NSTimeInterval, target aTarget: AnyObject, selector aSelector: Selector, userInfo: AnyObject?, repeats yesOrNo: Bool) -> NSTimer
在网上找了一些例子,但是好像都是之前的Bate版写的,试了几下没跑起来,现在就需要总结一下NSTimer计时的使用方法。
直接来代码吧,反正这个也不难
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timeFire", userInfo: nil, repeats: true) //定义了NSTimer对象 timer?.fire() //启动计时 func timeFire() { timer?.invalidate() //停止计时 }
咦,好像就是那么简单,注意timeFire这个函数没有参数