Ios 使按钮只点击一次-快速

Ios 使按钮只点击一次-快速,ios,swift,Ios,Swift,我在做计时器。我不知道如何使开始按钮只点击一次,因为它开始计数。并且在停止按钮计时器处。无效不起作用 @IBAction func start(_ sender: Any) { timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true) } @IBOutlet weak va

我在做计时器。我不知道如何使开始按钮只点击一次,因为它开始计数。并且在停止按钮计时器处。无效不起作用

@IBAction func start(_ sender: Any) {
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}

@IBOutlet weak var stopOutlet: UIButton!

@IBAction func stop(_ sender: Any) {
    seconds = 30
    label.text = "\(seconds)"
    timer.invalidate()
    audioPlayer.stop()
}

只需禁用按钮

@IBAction func start(_ sender: Any) {
    stopOutlet.isEnabled = false
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
然后在需要时重新启用:

stopOutlet.isEnabled = true

延伸。UIControl为所有控件提供此功能和更多功能。

只需禁用按钮即可

@IBAction func start(_ sender: Any) {
    stopOutlet.isEnabled = false
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
然后在需要时重新启用:

stopOutlet.isEnabled = true
延伸。UIControl为所有控件提供此功能和更多功能