我有一个使用计时器制作简单秒表的应用程序。然而,我有麻烦试图重置定时器为零。我在代码底部的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
设为零