Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IOS快速倒计时将不会停止_Ios_Swift_Timer_Swift2 - Fatal编程技术网

IOS快速倒计时将不会停止

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

我正在尝试制作一个倒计时计时器,它从60秒开始倒计时,然后在达到0时停止。但是,计时器持续进入负秒。任何建议都将不胜感激。代码:

@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分钟接受答案