gpt4 book ai didi

ios - 将倒数计时器值重置为数组中保存的默认值

转载 作者:行者123 更新时间:2023-11-29 05:08:17 26 4
gpt4 key购买 nike

我正在根据数组中的默认 Int 值创建倒计时器,但我不知道如何将计时器重置为数组中的原始值。这意味着当用户按下“停止冲泡”时,倒计时器将从 5 分钟重新开始。有人可以帮助我找出我做错或遗漏的地方吗?我正在迈出 iOS 开发的第一步。

提前致谢!

我的代码:

@objc func runTimer() {

cafe.time -= 1
labelCowntdownTimer.text = "\(cafe.time)"
// MM:SS:_

let flooredCounter = cafe.time

let minute = (flooredCounter % 3600) / 60
var minuteString = "\(minute)"
if minute < 10 {
minuteString = "0\(minute)"
}

let second = (flooredCounter % 3600) % 60
var secondString = "\(second)"
if second < 10 {
secondString = "0\(second)"
}

labelCowntdownTimer.text = "\(minuteString):\(secondString)"

}

//Mark: - resetTimer

@objc func resetTimer() {

timer.invalidate()
//cafe.time
runTimer()

}


//Mark: - ButtonPressed - Start Timer

@IBAction func startButtonPressed(_ sender: Any) {

if !isTimerRunning {

timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)

isTimerRunning = true
startButton.setTitle("Stop Brewing", for: .normal)
} else {
resetTimer()
isTimerRunning = false
startButton.setTitle("Start Brewing", for: .normal)

}
}

最佳答案

viewDidload() 中或每当您获取存储在数组中的值时,将该值存储在变量中。

var initialValueOfTime = 0   // the initial value is stored here. which is 300

override func viewDidLoad() {
super.viewDidLoad()
initialValueOfTime = cafe.time
}
@objc func runTimer() {

if isTimerRunning {
cafe.time -= 1
} else {
cafe.time = initialValueOfTime
}
labelCowntdownTimer.text = "\(cafe.time)"
// MM:SS:_

let flooredCounter = cafe.time

let minute = (flooredCounter % 3600) / 60
var minuteString = "\(minute)"
if minute < 10 {
minuteString = "0\(minute)"
}

let second = (flooredCounter % 3600) % 60
var secondString = "\(second)"
if second < 10 {
secondString = "0\(second)"
}

labelCowntdownTimer.text = "\(minuteString):\(secondString)"

}

//Mark: - resetTimer

@objc func resetTimer() {

timer.invalidate()
runTimer()
}


//Mark: - ButtonPressed - Start Timer

@IBAction func startButtonPressed(_ sender: Any) {

if !isTimerRunning {
isTimerRunning = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
startButton.setTitle("Stop Brewing", for: .normal)
} else {
isTimerRunning = false
resetTimer()
startButton.setTitle("Star Brewing", for: .normal)

}
}

关于ios - 将倒数计时器值重置为数组中保存的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59949543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com