Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a Button for 90 sec when pressed in swift

I have button that show up a modal view but i want that if the user click it he wont be able to use it again for 90 seconds. how can i do this?

like image 280
Ludyem Avatar asked Nov 05 '14 10:11

Ludyem


2 Answers

In the IBAction of the button disable the button and set a timer like this:

self.button.enabled = false
NSTimer.scheduledTimerWithTimeInterval(90, target: self, selector: "enableButton", userInfo: nil, repeats: false)

And create the func called when the timer ends counting:

func enableButton() {
    self.button.enabled = true
} 
like image 135
diegomen Avatar answered Oct 11 '22 20:10

diegomen


Swift 4

sender.isUserInteractionEnabled = false
Timer.scheduledTimer(withTimeInterval: 90, repeats: false, block: { _ in
    sender.isUserInteractionEnabled = true
})
like image 38
Vladimir Avatar answered Oct 11 '22 20:10

Vladimir