第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Swift中使用NSTimer?

如何在Swift中使用NSTimer?

米琪卡哇伊 2019-07-29 16:02:27
如何在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'
查看完整描述

3 回答

?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗 獲得超6個贊

更新到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)
    }}


查看完整回答
反對 回復(fù) 2019-07-29
?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗 獲得超6個贊

這將有效:


override func viewDidLoad() {

    super.viewDidLoad()

    // Swift block syntax (iOS 10+)

    let timer = Timer(timeInterval: 0.4, repeats: true) { _ in print("Done!") }

    // Swift >=3 selector syntax

    let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)

    // Swift 2.2 selector syntax

    let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)

    // Swift <2.2 selector syntax

    let timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)

}


// must be internal or public. 

@objc func update() {

    // Something cool

}

對于Swift 4,您希望獲取選擇器的方法必須公開給Objective-C,因此@objc必須將屬性添加到方法聲明中。


查看完整回答
反對 回復(fù) 2019-07-29
?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗 獲得超11個贊

重復(fù)的事件

您可以使用計時器多次執(zhí)行操作,如以下示例所示。計時器調(diào)用一種方法每半秒更新一次標(biāo)簽。

這是代碼:

import UIKitclass ViewController: UIViewController {

    var counter = 0
    var timer = Timer()

    @IBOutlet weak var label: UILabel!

    // start timer    @IBAction func startTimerButtonTapped(sender: UIButton) {
        timer.invalidate() // just in case this button is tapped multiple times
        // start the timer        timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
    }

    // stop timer    @IBAction func cancelTimerButtonTapped(sender: UIButton) {
        timer.invalidate()
    }

    // called every time interval from the timer    func timerAction() {
        counter += 1
        label.text = "\(counter)"
    }}

延遲事件

您還可以使用計時器在將來的某個時間安排一次性事件。與上述示例的主要區(qū)別在于您使用repeats: false而不是true。

timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false)

上面的示例調(diào)用delayedAction在設(shè)置計時器后兩秒鐘命名的方法。它不會重復(fù),但timer.invalidate()如果您需要在事件發(fā)生之前取消該事件,您仍然可以打電話。

筆記

  • 如果有可能多次啟動計時器實例,請確保先使舊計時器實例無效。否則你將失去對計時器的引用,你不能再停止它了。(見這個問答

  • 不需要時使用計時器。請參閱“iOS應(yīng)用能源效率指南”中的計時器部分。

有關(guān)


查看完整回答
反對 回復(fù) 2019-07-29
  • 3 回答
  • 0 關(guān)注
  • 1162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號