小编Pie*_* P.的帖子

停止并重新启动计时器

我想停止这个计时器,然后从我停止的地方重新启动它。

secondsTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(addSeconds), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

下面,有人建议我不应该在我的计时器处理程序中增加计时器。为什么不?

例如,使用 GCD 计时器:

func countSeconds() {

    secondsTimer = DispatchSource.makeTimerSource(queue: .main)
    secondsTimer?.schedule(deadline: .now(), repeating: 1.0)

    secondsTimer?.setEventHandler { [weak self] in

        self?.addSeconds()
    }
}

@objc func addSeconds() {
    seconds += 1                         
}

func startGame() {  
    secondsTimer?.resume()
}
Run Code Online (Sandbox Code Playgroud)

nstimer swift

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

nstimer ×1

swift ×1