ios - 快速计时器不会停止
问题描述
代码:
var timerCount: Int = 15
hideTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
self.timerCount -= 1
print(self.timerCount)
if self.timerCount <= 0 {
if self.hideTimer != nil {
self.hideTimer.invalidate()
self.hideTimer = nil
}
}
})
print 语句只是继续打印成负数 =(
这发生在 UITableViewCell 中,如果这很重要的话。
我究竟做错了什么?
解决方案
您在回调中获得对计时器的引用:'(timer)'。使用这个来使您的计时器无效:
var timerCount: Int = 3
let hideTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
timerCount -= 1
print(timerCount)
if timerCount <= 0 {
timer.invalidate()
}
})
推荐阅读
- r - 执行正确数据分析的步骤
- php - 表单操作正在刷新 html 页面,使其在域中生效
- php - 如何修复使用 file_get_html
- javascript - 在NodeJS中使用相同的参数在回调函数中调用父函数
- r - `tm` 包中的 `tm_map` 中的自定义函数
- css - 如何调整我的 blogspot 博客标题中的标题?
- haskell - optparse-applicative 非空字符串参数
- mysql - 如何使用条件计数器获取数据 Mysql 组
- reactjs - TypeScript 错误:无法调用类型缺少调用签名的表达式
- javascript - 如何通过从我的表单中的用户输入中检索数据来计算当前年龄?