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.
Asked
Active
Viewed 1.5k times
7 Answers
102
Set UIView.exclusiveTouch.

tc.
- 33,468
- 5
- 78
- 96
-
Awesome! Why I didn't think of that for buttons I do not know?! – happycodelucky Aug 13 '10 at 20:47
-
5Perfect ! that was like hidden treasure , never knew of this property, thanks a lot. – RVN Jul 14 '11 at 12:48
-
10please note to set it on each "UIButton"! NOT the the UIView those buttons are in :) (Set in on all subviews of that UIView would do) like this -> [self.controlView.subviews makeObjectsPerformSelector:@selector(setExclusiveTouch:) withObject:[NSNumber numberWithBool:YES]]; – Hlung Apr 27 '12 at 17:28
-
@Hlung: That won't necessarily work; `-setExclusiveTouch:` takes a BOOL, not a NSNumber (it might work if the number happens to be interpreted as YES, but that's not guaranteed). I call it `UIView.exclusiveTouch` because that's the class that the property is defined in (a UIButton is a UIView). – tc. Apr 30 '12 at 17:35
-
@tc. Yes, it is guaranteed. As far as I know, when I want to send a BOOL in `makeObjectsPerformSelector:withObject:`, sending any object with non-zero address will be interpreted as **YES** (yeah, `[NSNumber numberWithBool:NO]` will mean **YES**). And therefore, sending `nil` will be **NO**. In my example, I just want to make it clear that it is a YES. At least it's my style because I don't know a more proper way of doing it :) – Hlung May 02 '12 at 08:15
-
2@Hlung: It is *definitely not* guaranteed by the docs, which say "The method must take a single argument of type id". AIUI on x86, if the address is divisible by 256 then it will be equivalent to NO (since BOOL is a signed char so it only looks at the bottom 8 bits). I've also heard of UIKit interpreting 2 as NO (i.e. the code only looks at the bottom bit; unsurprising). – tc. May 11 '12 at 13:24
12
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

Nazik
- 8,696
- 27
- 77
- 123

Gajendra K Chauhan
- 3,387
- 7
- 40
- 55
4
Swift 4 Syntax:
buttonA.isExclusiveTouch = true
buttonB.isExclusiveTouch = true

Aaron Halvorsen
- 2,610
- 1
- 23
- 31
-
That answer does not really provide an addition to the already existing answers. – Striezel Jun 24 '18 at 12:35
2
for(UIView* v in self.view.subviews)
{
if([v isKindOfClass:[UIButton class]])
{
UIButton* btn = (UIButton*)v;
[yourButton setExclusiveTouch:YES];
}
}

Anurag Sharma
- 4,276
- 2
- 28
- 44
0
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)
}
}
}

Jorge Paiz
- 506
- 3
- 8
0
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;
}

SURESH SANKE
- 1,653
- 17
- 34
0
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

93sauu
- 3,770
- 3
- 27
- 43