1

我想知道如何在单击按钮后禁用按钮几秒钟。我可以用代码禁用它

button.enabled = button.enabled = 否;

但我不确定它是如何在几秒钟内完成的。

4

3 回答 3

6

使用此代码:

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

编辑:如果您想先禁用按钮并稍后执行一些代码,请执行以下操作:

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
});
于 2012-09-13T13:53:14.353 回答
1

您可以使用

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

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

将按钮设置为不可见后

于 2012-09-13T13:53:45.427 回答
0

感谢@Adam。

对于 Swift 3.0:

button.isEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(2000)) {
        btnCheckout.isEnabled = true
}
于 2017-09-07T13:51:12.747 回答