定时器频率选项
重复计时器事件
迅速
class ViewController: UIViewController {
var timer = NSTimer()
override func viewDidLoad() {
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector(self.timerMethod()), userInfo: nil, repeats: true)
}
func timerMethod() {
print("Timer method called")
}
func endTimer() {
timer.invalidate()
}
}
Swift 3
class ViewController: UIViewController {
var timer = Timer()
override func viewDidLoad() {
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerMethod()), userInfo: nil, repeats: true)
}
func timerMethod() {
print("Timer method called")
}
func endTimer() {
timer.invalidate()
}
}
如果需要,必须手动无效。
迅速
非重复延迟定时器事件
NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector(self.timerMethod()), userInfo: nil, repeats: false)
Swift 3
Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.timerMethod()), userInfo: nil, repeats: false)
定时器将在执行一次,3 秒后触发一次。一旦被解雇,将自动失效。