gpt4 book ai didi

swift - 每毫秒更新一次进度条

转载 作者:行者123 更新时间:2023-11-30 10:53:31 27 4
gpt4 key购买 nike

我希望我的进度栏每毫秒更新一次。我该怎么做?目前,它每秒更新一次,这不是我想要的。

编辑:抱歉,我忘了提及,我仍然希望计时器标签每秒更新一次(因此它会按秒而不是毫秒减少:10、9、8),同时每毫秒或每 25 次更新进度条第二。

代码:

 progressBar.transform = progressBar.transform.scaledBy(x: 1, y: 5)
progressBar.layer.cornerRadius = 5
progressBar.clipsToBounds = true
progressBar.layer.sublayers![1].cornerRadius = 5
progressBar.subviews[1].clipsToBounds = true

func startTimer() {

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

}

@objc func timerUpdate() {

if timeRemaining <= 0 {
progressBar.setProgress(Float(0), animated: false)
bonusTimerLabel.text = "0"
bonusTimerLabel.textColor = UIColor(red: 186/255, green: 16/255, blue: 16/255, alpha: 1)

} else {
progressBar.setProgress(Float(timeRemaining)/Float(10), animated: false)
timeRemaining -= 1
bonusTimerLabel.text = "\(timeRemaining)"
}

最佳答案

不建议每毫秒使用timer触发一个函数,引用:https://stackoverflow.com/a/30983444/8447312

因此,为了安全起见,您可以每 50 毫秒触发一次计时器函数并更新进度条。但这不应该太明显。

还要确保 timeRemainingDouble,然后尝试:

func startTimer() {

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

}

@objc func timerUpdate() {

if timeRemaining <= 0 {
progressBar.setProgress(Float(0), animated: false)
bonusTimerLabel.text = "0"
bonusTimerLabel.textColor = UIColor(red: 186/255, green: 16/255, blue: 16/255, alpha: 1)

} else {
progressBar.setProgress(Float(timeRemaining)/Float(20), animated: false)
timeRemaining -= 0.050
bonusTimerLabel.text = "\(Int(timeRemaining))"
}

关于swift - 每毫秒更新一次进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207910/

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