Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set exclusive touch on multiple UIViews of the same class

Tags:

ios

uiview

touch

I am creating a random number of custom UIViews of the same class, and I'm adding them in the UIViewController's view. I'm assigning them a UITapGestureRecognizer, but I can't seem to make the exclusive touch work:

for (int i = 0; i <= n; i++) {
   ICCatalogProductView *catalogProductView;
   catalogProductView = [[ICCatalogProductView alloc] init];
   [self.view addSubview:catalogProductView]
   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testTouch)];
   [catalogProductView addGestureRecognizer:tapGesture];
   [catalogProductView setExclusiveTouch:YES];
}

If i tap the UIViews simultanously, the method is called twice (not the behaviour I want). Is there any elegant method of solving this, or any method at all?

like image 325
Durican Radu Avatar asked Jul 25 '13 11:07

Durican Radu


1 Answers

From the Apple Documentation:

exclusiveTouch only prevents touches in other views during the time in which there's an active touch in the exclusive touch view. That is, if you put a finger down in an exclusive touch view touches won't start in other views until you lift the first finger. It does not prevent touches from starting in other views if there are currently no touches in the exclusiveTouch view.

To truly make this view the only thing on screen that can receive touches you'd need to either add another view over top of everything else to catch the rest of the touches, or subclass a view somewhere in your hierarchy (or your UIWindow itself) and override hitTest:withEvent: to always return your text view when it's visible, or to return nil for touches not in your text view.

means its only set exclusive in your one view, not if you are touching something outside your view.

like image 96
Julien Klindt Avatar answered Nov 26 '22 20:11

Julien Klindt