类中的 Swift 3 计时器未触发

标签 swift timer

我有一个类:

class GameManager {...

在其中我有这个功能:

func startGame() {

        msgTimer = Timer(timeInterval: 0.5, target: self, selector: #selector(typeMessage(_:)), userInfo: nil, repeats: true)

}

及其调用的选择器:

@objc func typeMessage(_ sender:Timer) {

        if textCount > strInitText.characters.count {
            let strThisChar = strInitText[strInitText.index(strInitText.startIndex, offsetBy: textCount)]
            strDisplayText = strDisplayText + String(strThisChar)
            print(strDisplayText)
        }

    }

但是选择器永远不会被调用。

enter image description here

最佳答案

改变

msgTimer = Timer(timeInterval: 0.5, target: self, selector: #selector(typeMessage(_:)), userInfo: nil, repeats: true)

msgTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(typeMessage(_:)), userInfo: nil, repeats: true)

关于类中的 Swift 3 计时器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611843/

相关文章:

swift - 在 Swift 中设置类变量的要求

javascript - 将 UTC 时间添加到我的倒计时器

ios - 向特定用户发送通知

ios - 使用场景的shouldRasterize属性

ios - 如何快速更改字符串中所有数字的颜色

Java Swing Timer 只运行一次

Android 加速度计设置 : can I get it to run only when button it pressed.

java - 在java中处理定时器

c++ - 如何在 popen 函数 C++ 中插入计时器?

ios - 在不崩溃的情况下被拒绝后提示用户允许定位服务