单击按钮后禁用一段时间(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
After clicking button make it disabled for a while (XCode)
提问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
}