重置swift定时器



我有一个使用计时器制作简单秒表的应用程序。然而,我有麻烦试图重置定时器为零。我在代码底部的resettingtimer函数专门用于此。但是,我不能将其设置为零,因为它接受一个字符串值。

//STOPWATCH UP SECTION---------
func activateCountUpTimer() {
timerCounting = true
timerUp = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerCounter), userInfo: nil, repeats: true)
}

@IBAction func pauseCoutUpTimer(_ sender: Any) {

if timerCounting == true {
timerUp.invalidate()
timerCounting = false
//here to change the uibutton image from pause to reactivate when clicked
} else {
timerCounting = true
timerUp = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerCounter), userInfo: nil, repeats: true)
}

}

@objc func timerCounter() -> Void
{
count = count + 1
let time = secondsToHoursMinutesSeconds(seconds: count)
let timeString = makeTimeString(hours: time.0, minutes: time.1, seconds: time.2)
stopwatchLabel.text = timeString
}

func secondsToHoursMinutesSeconds(seconds: Int) -> (Int,Int,Int){
return ((seconds / 3600), ((seconds % 3600)/60), ((seconds % 3600) % 60))
}

func makeTimeString(hours: Int, minutes: Int, seconds: Int) -> String {
var timeString = ""
timeString += String(format: "%02d", hours)
timeString += " : "
timeString += String(format: "%02d", minutes)
timeString += " : "
timeString += String(format: "%02d", seconds)
return timeString
}

func resettingtimer() {
timerUp = 0
}
//END OF STOPWATCH SECTION---------

使timerUp.invalidate()定时器失效,timerCounting设为假,count设为零,stopwatchLabel.text设为零

最新更新