单击按钮后禁用一段时间(XCode)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12407740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:29:11  来源:igfitidea点击:

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

objective-cxcode

提问by TomasJ

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;

button.enabled = button.enabled = NO;

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

但我不确定如何在短短几秒钟内完成。

回答by Adam

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

回答by Casabian

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

将按钮设置为不可见后

回答by Amit

Thanks to @Adam.

感谢@Adam。

For Swift 3.0 :

对于 Swift 3.0 :

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