views:

114

answers:

2

Hey folks,

I am currently developing an app where I have kind of a main view with lots of subviews. Now I have two specific subviews and I want only one of them to be "touchable" at once. So both should be able to response to a touch event but it should not be able that both respond simultaneously. Since I have other subview which should be able to touch at the same time I cannot use the multitouch or exclusiveTouch property. Any ideas on this? Maybe I could kind of group both view to a single-touch area.

Thanks in advance

A: 

Put logic in their parent that (temporarily) disables the one that didn't get the touch (userInteractionEnabled = NO).

Of course, someone could still put a finger down on each of them simultaneously. You could figure which view is closest to the center of the touches.

David Dunham
I did disable one view with userInteractionEnabled when the other received a touchesBegan event but as you said it is still possible to press them both simultaneously.I also tried sending a touchesEnded event to the view which got touched secondly but this didn't work at all.
Russo
Although they will be pressed simultaneously, they cannot both be sent touchesBegan simultaneously, which is why Lawrence's suggestion should work.
David Dunham
+1  A: 

Stick a read-write property, let's call it viewBeingTouched, in your app delegate or some other singleton. Make sure you use @property without nonatomic.

Make your two views instances of a class that overrides the UIResponder methods thusly:

  1. In touchesBegan, check the value of viewBeingTouched. If it's nil, set it to self and call the super's implementation. Otherwise, do nothing.

  2. In touchesEnded, check the value of viewBeingTouched. If it's self, set it to nil.

Get it? The first view to get a touch grabs the property and prevents the other from registering it.

lawrence
Thx, I think I got it. I will try it out today in the evening.
Russo