Button 在swift中按下按钮时,禁用按钮90秒
我有一个按钮,显示一个模态视图,但我想,如果用户点击它,他将无法再次使用90秒。如何执行此操作?在按钮的i操作中,禁用按钮并设置如下计时器:Button 在swift中按下按钮时,禁用按钮90秒,button,time,nstimer,Button,Time,Nstimer,我有一个按钮,显示一个模态视图,但我想,如果用户点击它,他将无法再次使用90秒。如何执行此操作?在按钮的i操作中,禁用按钮并设置如下计时器: self.button.enabled = false NSTimer.scheduledTimerWithTimeInterval(90, target: self, selector: "enableButton", userInfo: nil, repeats: false) 并创建计时器结束计数时调用的func: func enableButto
self.button.enabled = false
NSTimer.scheduledTimerWithTimeInterval(90, target: self, selector: "enableButton", userInfo: nil, repeats: false)
并创建计时器结束计数时调用的func:
func enableButton() {
self.button.enabled = true
}
#Swift 3
在要禁用按钮的位置编写此代码
self.buttonTest.isEnabled = false
Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(ViewController.enableButton), userInfo: nil, repeats: false)
这里buttonTest是该按钮的出口
并在ViewController中的任意位置编写此代码
func enableButton() {
self.buttonTest.isEnabled = true
}
让我知道任何澄清。谢谢。Swift 3
我希望这个答案更加通用,以便开发人员发现它更有帮助
首先,按钮是挂在插座上还是作为一个动作?
在这两种情况下,您都需要将其连接为插座
其次,我建议您使用闭包而不是编写func,然后调用它,您只需执行以下操作
@IBOutlet weak var buttonWithTimer: UIButton!{
didSet{
self.buttonWithTimer.isEnabled = false
Timer.scheduledTimer(withTimeInterval: 90, repeats: false) {
[weak self]timer in
self?.buttonWithTimer.isEnabled = true
} // [weak self]inside the closure is to break a possible
// memory sicle
}
}
斯威夫特4
sender.isUserInteractionEnabled = false
Timer.scheduledTimer(withTimeInterval: 90, repeats: false, block: { _ in
sender.isUserInteractionEnabled = true
})