gpt4 book ai didi

ios - swift |计时器不会在无效调用时停止,而是加速?

转载 作者:行者123 更新时间:2023-11-28 10:33:41 25 4
gpt4 key购买 nike

我在此应用程序中使用计时器制作了一个秒表,并添加了开始停止按钮以暂停和播放相同的内容。当按下按钮时,它被发送到一个使计时器无效的函数,它应该停止。但奇怪的是,当按下停止按钮时,计时器并没有停止而是以某种方式加速除了一次声明之外,我没有更改时间间隔。

我曾尝试在按下开始按钮后将其禁用,甚至将其隐藏。还尝试更改时间间隔但没有任何效果。我越按开始停止按钮,它越加速,开始比提到的时间间隔快得多。

startButton.frame = CGRect(x: 0, y: UIScreen.main.bounds.height * 0.9 , width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.1)
startButton.setTitle("Start Timer", for: .normal)
self.view.addSubview(startButton)
startButton.setTitleColor(.white , for: .normal)
startButton.backgroundColor = .red
startButton.addTarget(self, action: #selector(playButton(_:)), for: .allTouchEvents)

stopButton.frame = CGRect(x: 0, y: UIScreen.main.bounds.height * 0.9 , width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.1)
stopButton.setTitle("Stop Timer", for: .normal)
stopButton.setTitleColor(.white , for: .normal)
stopButton.backgroundColor = .red
stopButton.addTarget(self, action: #selector(pauseButton(_:)), for: .allTouchEvents)
@objc func playButton(_ sender : Any)
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self , selector: #selector(updateTimer), userInfo: nil, repeats: true)
startButton.isEnabled = false
stopButton.isEnabled = true
isRunning = true
self.view.addSubview(stopButton)
startButton.isHidden = true
stopButton.isHidden = false



}

@objc func pauseButton(_ sender: Any) {
self.view.addSubview(startButton)
timer.invalidate()

stopButton.isHidden = true
startButton.isHidden = false

startButton.isEnabled = true
stopButton.isEnabled = false

isRunning = false

}


@objc func updateTimer(_ sender : Any)
{
counter += 0.1
titleLabel.text = String(format: "%.1f", counter)
}

最佳答案

据我所知,你有 2 个错误。

另一个答案提到了第一个。这是一个真实的建议,你不应该总是添加一个新的 UIButton 并且应该只为每个按钮使用 hide/unhide 属性。

第二个错误在于添加目标的方式。您正在使用 .allTouchEvents,但是您可能打算使用 .touchUpInside 作为您的控制状态。

请参阅以下更正后的代码供您引用:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var titleLabel: UILabel!

var startButton: UIButton!
var stopButton: UIButton!
var timer: Timer!
var counter: Double = 0.0

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

startButton = UIButton(frame: CGRect(x: 0, y: UIScreen.main.bounds.height * 0.9 , width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.1))
startButton.setTitle("Start Timer", for: .normal)
startButton.setTitleColor(.white , for: .normal)
startButton.backgroundColor = .red
startButton.addTarget(self, action: #selector(playButton(_:)), for: .touchUpInside)
self.view.addSubview(startButton)
self.startButton.isHidden = false

stopButton = UIButton(frame: CGRect(x: 0, y: UIScreen.main.bounds.height * 0.9 , width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.1))
stopButton.setTitle("Stop Timer", for: .normal)
stopButton.setTitleColor(.white , for: .normal)
stopButton.backgroundColor = .red
stopButton.addTarget(self, action: #selector(pauseButton(_:)), for: .touchUpInside)
self.view.addSubview(stopButton)
self.stopButton.isHidden = true
}

@objc func playButton(_ sender : Any) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self , selector: #selector(updateTimer), userInfo: nil, repeats: true)

startButton.isEnabled = false
stopButton.isEnabled = true
startButton.isHidden = true
stopButton.isHidden = false
}

@objc func pauseButton(_ sender: Any) {
timer.invalidate()

stopButton.isHidden = true
startButton.isHidden = false
startButton.isEnabled = true
stopButton.isEnabled = false
}


@objc func updateTimer(_ sender : Any)
{
counter += 0.1
titleLabel.text = String(format: "%.1f", counter)
}
}

关于ios - swift |计时器不会在无效调用时停止,而是加速?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54766523/

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