IOS快速倒计时将不会停止
我正在尝试制作一个倒计时计时器,它从60秒开始倒计时,然后在达到0时停止。但是,计时器持续进入负秒。任何建议都将不胜感激。代码:IOS快速倒计时将不会停止,ios,swift,timer,swift2,Ios,Swift,Timer,Swift2,我正在尝试制作一个倒计时计时器,它从60秒开始倒计时,然后在达到0时停止。但是,计时器持续进入负秒。任何建议都将不胜感激。代码: @IBOutlet var timeCounter: UILabel! var second = 60 var timer = NSTimer() var timerRunning = true override func viewDidLoad() { super.viewDidLoad() // Do any additional setu
@IBOutlet var timeCounter: UILabel!
var second = 60
var timer = NSTimer()
var timerRunning = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setTimer()
timerCounting()
}
func setTimer(){
second -= 1
timeCounter.text = "\(second)"
}
func timerCounting(){
if(timerRunning == true){
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("setTimer"), userInfo: nil, repeats: true)
timerRunning = true
if second == 0 {
timerRunning = false
timer.invalidate()
}
}
}
您必须将失效转移到
setTimer
函数中,因为它的当前位置将永远不会被触发,因为timerCounting
在开始时只被调用一次
func setTimer(){
second -= 1
timeCounter.text = "\(second)"
if second == 0 {
timerRunning = false
timer.invalidate()
}
}
您必须将失效转移到
setTimer
函数中,因为它的当前位置将永远不会被触发,因为timerCounting
在开始时只被调用一次
func setTimer(){
second -= 1
timeCounter.text = "\(second)"
if second == 0 {
timerRunning = false
timer.invalidate()
}
}
在操场上玩这个
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
@objc class C:NSObject {
var timer: NSTimer?
var second = 5
func setTimer(){
if c.second < 0 {
print("the timer fires the last time here ...")
timer?.invalidate()
} else {
print(second)
second -= 1
}
}
}
let c = C()
func timerCounting(){
c.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: c, selector: Selector("setTimer"), userInfo: nil, repeats: true)
}
timerCounting()
导入
XCPlaygroundPage.currentPage.needsIndefiniteExecution=true
进口基金会
@objc C类:NSObject{
变量计时器:NSTimer?
var秒=5
func setTimer(){
如果c.second<0{
打印(“计时器上次在此触发…”)
计时器?.invalidate()
}否则{
打印(秒)
秒-=1
}
}
}
设c=c()
func timerCounting(){
c、 timer=NSTimer.scheduledTimerWithTimeInterval(1,目标:c,选择器:选择器(“setTimer”),userInfo:nil,repeats:true)
}
时间计数()
在操场上玩这个
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
import Foundation
@objc class C:NSObject {
var timer: NSTimer?
var second = 5
func setTimer(){
if c.second < 0 {
print("the timer fires the last time here ...")
timer?.invalidate()
} else {
print(second)
second -= 1
}
}
}
let c = C()
func timerCounting(){
c.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: c, selector: Selector("setTimer"), userInfo: nil, repeats: true)
}
timerCounting()
导入
XCPlaygroundPage.currentPage.needsIndefiniteExecution=true
进口基金会
@objc C类:NSObject{
变量计时器:NSTimer?
var秒=5
func setTimer(){
如果c.second<0{
打印(“计时器上次在此触发…”)
计时器?.invalidate()
}否则{
打印(秒)
秒-=1
}
}
}
设c=c()
func timerCounting(){
c、 timer=NSTimer.scheduledTimerWithTimeInterval(1,目标:c,选择器:选择器(“setTimer”),userInfo:nil,repeats:true)
}
时间计数()
太棒了,谢谢你,这成功了,再等4分钟接受答案太棒了,谢谢你,这成功了,再等4分钟接受答案