happycodelucky
happycodelucky

Reputation: 1011

Preventing multiple buttons from being touched at the same time

In iOS is there anyway to prevent a UIView containing multiple buttons (siblings) from being simultaneously from being touched? For instance, two non-overlapping buttons that are side by side can be tapped at the same time with two touches.

Upvotes: 41

Views: 15842

Answers (7)

93sauu
93sauu

Reputation: 4127

To enable the exclusive touch you need to set the property isExclusiveTouch for each element.

myView.isExclusiveTouch = true

If you would want that the default behaviour changes you could use the UIAppearance of the UIView class.

UIView.appearance().isExclusiveTouch = true

Upvotes: 0

SURESH SANKE
SURESH SANKE

Reputation: 1663

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

Upvotes: 0

Aaron Halvorsen
Aaron Halvorsen

Reputation: 2680

Swift 4 Syntax:

    buttonA.isExclusiveTouch = true
    buttonB.isExclusiveTouch = true

Upvotes: 4

Jorge Paiz
Jorge Paiz

Reputation: 516

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

Upvotes: 0

Anurag Sharma
Anurag Sharma

Reputation: 4855

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

Upvotes: 2

Gajendrasinh Chauhan
Gajendrasinh Chauhan

Reputation: 3397

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

Upvotes: 12

tc.
tc.

Reputation: 33602

Set UIView.exclusiveTouch.

Upvotes: 102

Related Questions