如何防止 iOS 中同一个 UIButton 发生多个事件?

2023-12-30

我想防止连续多次点击同一个UIButton.

我尝试过enabled and exclusiveTouch属性,但没有用。例如:

-(IBAction) buttonClick:(id)sender{
    button.enabled = false;
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
        // code to execute
     }
     completion:^(BOOL finished){
         // code to execute  
    }];
    button.enabled = true;
}

您所做的是,只需在块之外设置启用/关闭即可。这是错误的,一旦调用该方法,它就会执行,因此在调用完成块之前不会禁用按钮。相反,您应该在动画完成后重新启用它。

-(IBAction) buttonClick:(id)sender{
    button.enabled = false;
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
        // code to execute
     }
     completion:^(BOOL finished){
         // code to execute  
        button.enabled = true; //This is correct.
    }];
    //button.enabled = true; //This is wrong.
}

哦,是的,而不是true and false, YES and NO看起来不错。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何防止 iOS 中同一个 UIButton 发生多个事件? 的相关文章

随机推荐