[英]Swift timer won't stop
Code:代码:
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 statement just keeps on printing into negative numbers =( print 语句只是继续打印成负数 =(
This is happening within a UITableViewCell, if that matters.这发生在 UITableViewCell 中,如果这很重要的话。
What am I doing wrong?我究竟做错了什么?
You get a reference to the timer in the callback: '(timer)'.您在回调中获得对计时器的引用:'(timer)'。 Use this one to invalidate your 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()
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.