我在计时器的 Collection View 中有一个计时器,它会滴答作响,但每次我滑动时,在滑动中间,计时器就会停止
正如您在图片中看到的那样,当您滑动时计时器静止不动,它只会在您完成滑动时开始滴答作响。这是代码:
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(QuestionsViewController.update), userInfo: nil, repeats: true)
self.navigationItem.setHidesBackButton(true, animated: true)
func update() {
if counter >= 0 {
self.title = "Time Left: \(String(self.counter))"
counter -= 1
} else {
alert()
timer!.invalidate()
}
}
我做错了什么?
最佳答案
我建议将计时器添加到主运行循环中:
RunLoop.main.add(timer, forMode: .commonModes)
关于swift - 计时器不会在 CollectionView 中正确更新 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516516/