如何在iOS中防止同一UIButton上发生多个事件? [英] How to prevent multiple event on same UIButton in iOS?

查看:63
本文介绍了如何在iOS中防止同一UIButton上发生多个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想防止在同一 UIButton 上连续多次单击.

I want to prevent continuous multiple clicks on the same UIButton.

我尝试使用 enabled exclusiveTouch 属性,但是没有用.如:

I tried with enabled and exclusiveTouch properties but it didn't work. Such as:

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

推荐答案

您正在做的是,您只需在块外设置启用/禁用启用.这是错误的,一旦该方法将被调用,它就会执行,因此在调用完成块之前,它不会禁用按钮.相反,一旦动画制作完成,您应该重新启用它.

What you're doing is, you simply setting enabled on/off outside of the block. This is wrong, its executing once this method will call, thus its not disabling the button until completion block would call. Instead you should reenable it once your animation would get complete.

-(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 false YES NO 看起来不错.:)

Oh and yes, instead of true and false, YES and NO would looks nice. :)

这篇关于如何在iOS中防止同一UIButton上发生多个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆