TomasJ
TomasJ

Reputation: 279

After clicking button make it disabled for a while (XCode)

I'd like to know how can I make a button disable for few seconds after clicking it. I can disable it with code

button.enabled = button.enabled = NO;

But I'm not sure how it can be done for just few seconds.

Upvotes: 1

Views: 5512

Answers (3)

Amit
Amit

Reputation: 4886

Thanks to @Adam.

For Swift 3.0 :

button.isEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(2000)) {
        btnCheckout.isEnabled = true
}

Upvotes: 0

Adam
Adam

Reputation: 26917

Use this code:

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    button.enabled = NO;
});

EDIT: If you want to disable your button first and execute some code later on, do this:

button.enabled = NO;
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //this will be executed after 2 seconds
});

Upvotes: 6

Casabian
Casabian

Reputation: 308

you can use

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setButtonEnabled) userInfo:nil repeats:NO];

-(void)setButtonEnabled{
    [myButton setEnabled:YES]
}

after you set the button invisible

Upvotes: 1

Related Questions