簡體   English   中英

斯威夫特| 計時器不會在無效呼叫上停止,而是加快速度嗎?

[英]Swift | The timer wont stop on invalidate call, rather speeds up?

我已經在此應用中使用Timer進行了秒表,並添加了“開始停止”按鈕以暫停並播放相同內容。 當按下按鈕時,它將發送到使計時器無效的功能,並且應該停止。 但是當按下“停止”按鈕時,奇怪的是,而不是以某種方式停止計時器,我沒有改變時間間隔,只是一次聲明它。

我曾嘗試禁用啟動按鈕,一旦按下它甚至將其隱藏。 也嘗試更改時間間隔,但沒有任何效果。 我按啟動停止按鈕的次數越多,它的速度就越快,並且開始運行的速度比上述時間間隔快得多。

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)
}

嘗試通過隱藏停止按鈕來同時添加兩個按鈕,並在單擊按鈕時隱藏和取消隱藏按鈕。 當您嘗試停止計時器時,您的播放按鈕方法每次都會運行

據我所知,您有兩個錯誤。

另一個答案提到第一個。 確實建議您不要總是添加新的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)
}
}

您已經有2個正確答案,但是我會投入2p,因為我認為您開始的前提不正確。

您最大的錯誤是首先擁有2個按鈕。 如果您只有一個按鈕並根據需要設置樣式,則不會有問題。

class MyTest {
    let magicButton = UIButton()
    let timer: Timer?

    override viewDidLoad() {
        super.viewDidLoad()

        // Setup Button
        magicButton.addTarget(self, action: #selector(buttonPress(_:)), for: .allTouchEvents)
        magicButton.setTitleColor(.white , for: .normal)
        magicButton.frame = yourFrame
        view.addSubview(magicButton)

        customiseButton()
    }

    @objc private func buttonPress() {
        if timer == nil {
            timer = Timer.scheduledTimer(timeInterval: 1, target: self , selector: #selector(updateTimer), userInfo: nil, repeats: true)
        } else {
            timer.invalidate()
        }

        customiseButton()
    }

    private func customiseButton() {
        let isStartButton = timer == nil
        let buttonTitle = isStartButton ? "Start" : "Stop"
        let buttonBackgroundColor: UIColor = isStartButton ? .green : .red

        magicButton.setTitle(buttonTitle, for: .normal)
        magicButton.backgroundColor = buttonBackgroundColor
    }
}

// add updateTimer function too

這樣,您需要維護的代碼更少,並且隱藏/顯示的內容之間沒有沖突,因此可以減少出錯的地方。 神奇的事情發生在buttonPress方法中,如果啟動了計時器,則將其停止,否則將啟動該計時器,然后快速更新按鈕UI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM