Preventing multiple buttons from being touched at the same time

99封情书 提交于 2019-12-02 17:54:42

Set UIView.exclusiveTouch.

Gajendra K Chauhan

You can also use below method. If you have two buttons or more, to prevent multiple push at a time.

for e.g,

[Button1 setExclusiveTouch:YES];

[Button2 setExclusiveTouch:YES];

Set this method in your viewDidLoad or viewWillAppear

for(UIView* v in self.view.subviews)
    {
    if([v isKindOfClass:[UIButton class]])
    {
        UIButton* btn = (UIButton*)v;
        [yourButton setExclusiveTouch:YES];
    }
}

Swift 4 Syntax:

    buttonA.isExclusiveTouch = true
    buttonB.isExclusiveTouch = true

You need to find all buttons on that view and set the "exclusiveTouch" property to true in order to prevent multi touch at the same time.

func exclusiveTouchForButtons(view: UIView) {
    for cmp in view.subviews {
        if let cmpButton = cmp as? UIButton {
            cmpButton.exclusiveTouch = true
        } else {
            exclusiveTouchForButtons(cmp)
        }
    }
}
I have tried both  multiTouchEnabled and exclusiveTouch but unfortunately 
none of them workout for me.I have tried the following code worked 
perfectly.

Set this code at .h file

BOOL ClickedBool;

Set the following code at method start.

if(ClickedBool==TRUE)
{
     return;
}
else
{
    ClickedBool=TRUE;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!