views:

548

answers:

1

My application has two overlapping UIButtons. The top button may be disabled at times. However, in this case any touch events it receives seem to be passed down to the underlying view, which in my case is the other button.

What I need is the top button intercepting all touches and preventing them from reaching the bottom button, even in disabled state (I would be happy even if it's designated action is invoked in the disabled state).

So far I've tried:

[topButton setUserInteractionEnabled:YES];

and

[topButton setExclusiveTouch:YES];

though the later case is probably undesirable since I still need the bottom button responding to events if it's the first view clicked. Either way none of them work.

A: 

Create a subclass of the button, then override these methods so that the button will catch events but ignore them if some property of the button is set to NO:

touchesBegan:withEvent:
touchesCancelled:withEvent:
touchesEnded:withEvent:
touchesMoved:withEvent:

These are methods of UIResponder.

Have them just call their [super ...] method if you want the event handled, otherwise just don't call it and return if you want the events "eaten".

Also see this in case it's necessary: http://stackoverflow.com/questions/2003201/observing-pinch-multi-touch-gestures-in-a-uitableview/2003781#2003781

Nimrod
Looks like these methods aren't called at all when the button is disabled.
MihaiD
Yea, that's what I figured (which is why I said "some property") which is why you may need to either use the second method, or do something other than disable the button like simulate it being disabled somehow. The second method probably will be a pain since the UIApplication (which calls sendEvent in the window) probably is what's ignoring disabled UIControls.
Nimrod