如何在Swift中使用NSTimer?

如何在Swift中使用NSTimer?

我试过了

var timer = NSTimer()timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false)

但是,我说错了

'(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6' is not identical to 'NSTimer'


米琪卡哇伊
浏览 1125回答 3
3回答

红糖糍粑

更新到Swift 4,利用userInfo:class TimerSample {     var timer: Timer?     func startTimer() {         timer = Timer.scheduledTimer(timeInterval: 5.0,                                      target: self,                                      selector: #selector(eventWith(timer:)),                                      userInfo: [ "foo" : "bar" ],                                      repeats: true)     }     // Timer expects @objc selector    @objc func eventWith(timer: Timer!) {         let info = timer.userInfo as Any         print(info)     }}

哈士奇WWW

这将有效:override func viewDidLoad() {&nbsp; &nbsp; super.viewDidLoad()&nbsp; &nbsp; // Swift block syntax (iOS 10+)&nbsp; &nbsp; let timer = Timer(timeInterval: 0.4, repeats: true) { _ in print("Done!") }&nbsp; &nbsp; // Swift >=3 selector syntax&nbsp; &nbsp; let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)&nbsp; &nbsp; // Swift 2.2 selector syntax&nbsp; &nbsp; let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)&nbsp; &nbsp; // Swift <2.2 selector syntax&nbsp; &nbsp; let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)}// must be internal or public.&nbsp;@objc func update() {&nbsp; &nbsp; // Something cool}对于Swift 4,您希望获取选择器的方法必须公开给Objective-C,因此@objc必须将属性添加到方法声明中。

慕尼黑8549860

重复的事件您可以使用计时器多次执行操作,如以下示例所示。计时器调用一种方法每半秒更新一次标签。这是代码:import&nbsp;UIKitclass&nbsp;ViewController:&nbsp;UIViewController&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;counter&nbsp;=&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;timer&nbsp;=&nbsp;Timer() &nbsp;&nbsp;&nbsp;&nbsp;@IBOutlet&nbsp;weak&nbsp;var&nbsp;label:&nbsp;UILabel! &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;start&nbsp;timer&nbsp;&nbsp;&nbsp;&nbsp;@IBAction&nbsp;func&nbsp;startTimerButtonTapped(sender:&nbsp;UIButton)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.invalidate()&nbsp;//&nbsp;just&nbsp;in&nbsp;case&nbsp;this&nbsp;button&nbsp;is&nbsp;tapped&nbsp;multiple&nbsp;times &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;start&nbsp;the&nbsp;timer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer&nbsp;=&nbsp;Timer.scheduledTimer(timeInterval:&nbsp;0.5,&nbsp;target:&nbsp;self,&nbsp;selector:&nbsp;#selector(timerAction),&nbsp;userInfo:&nbsp;nil,&nbsp;repeats:&nbsp;true) &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;stop&nbsp;timer&nbsp;&nbsp;&nbsp;&nbsp;@IBAction&nbsp;func&nbsp;cancelTimerButtonTapped(sender:&nbsp;UIButton)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timer.invalidate() &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;called&nbsp;every&nbsp;time&nbsp;interval&nbsp;from&nbsp;the&nbsp;timer&nbsp;&nbsp;&nbsp;&nbsp;func&nbsp;timerAction()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter&nbsp;+=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label.text&nbsp;=&nbsp;"\(counter)" &nbsp;&nbsp;&nbsp;&nbsp;}}延迟事件您还可以使用计时器在将来的某个时间安排一次性事件。与上述示例的主要区别在于您使用repeats: false而不是true。timer&nbsp;=&nbsp;Timer.scheduledTimer(timeInterval:&nbsp;2.0,&nbsp;target:&nbsp;self,&nbsp;selector:&nbsp;#selector(delayedAction),&nbsp;userInfo:&nbsp;nil,&nbsp;repeats:&nbsp;false)上面的示例调用delayedAction在设置计时器后两秒钟命名的方法。它不会重复,但timer.invalidate()如果您需要在事件发生之前取消该事件,您仍然可以打电话。笔记如果有可能多次启动计时器实例,请确保先使旧计时器实例无效。否则你将失去对计时器的引用,你不能再停止它了。(见这个问答)不需要时使用计时器。请参阅“iOS应用能源效率指南”中的计时器部分。有关如何使用Swift中的日期和时间
打开App,查看更多内容
随时随地看视频慕课网APP