ios - 无效和重新启动后,Swift 计时器将无法工作

标签 ios swift timer nstimer

我正在使用 Timer() 指示音频播放器中的当前音频位置

func startTimer() {
        print("PlayerController: startTimer()")
        if itemPlayTimer != nil {
            return
        }
        itemPlayTimer = Timer.scheduledTimer(timeInterval: 0.001,
                                                  target: self,
                                                  selector: #selector(updateItemPlayerTimer),
                                                  userInfo: nil,
                                                  repeats: true)
    }

 @objc func updateItemPlayerTimer() {
        guard let currentTime = player?.currentTime else {
            return
        }
        updateTimeDescription?(currentTime)
    }

当用户暂停播放器应用使计时器失效时

func stopTimer() {
        itemPlayTimer?.invalidate()
        itemPlayTimer = nil
    }

但是在再次调用 startTimer() 之后选择器不会调用

最佳答案

原因是定时器开始在其他线程中执行,而不是在主线程中。

关于ios - 无效和重新启动后,Swift 计时器将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50211687/

相关文章:

timer - 测试片段不执行计时器或示例超时

ios - 仅在键盘上显示字母(禁用数字和标点符号)

ios - iOS 7 上的 NS 合并冲突

ios - 从另一个 View Controller 更新 UIPageControl

swift - 倒计时代码在 Swift 2 中不起作用

matlab - 从 MATLAB 中附加的 .CSV 中提取最新值

android - 在没有 Facebook、Twitter 等的情况下,如何将社交功能集成到 iOS 和/或 Android 应用程序中?

ios - Swift 如何在按钮单击时显示 Tabbar

ios - 当从 iOS 13.1 中的 SceneDelegate 设置 RootViewController 时,UIElements 变为非事件状态

按下按钮时的 Java 计时器