views:

222

answers:

3

Let's say we have a view controller with one sub view. the subview takes up the center of the screen with 100 px margins on all sides. We then add a bunch of little stuff to click on inside that subview. We are only using the subview to take advantage of the new frame ( x=0, y=0 inside the subview is actually 100,100 in the parent view).

Then, imagine that we have something behind the subview, like a menu. I want the user to be able to select any of the "little stuff" in the subview, but if there is nothing there, I want touches to pass through it (since the background is clear anyway) to the buttons behind it.

How can I do this? It looks like touchesBegan goes through, but buttons don't work.

A: 

As far as I know, you are supposed to be able to do this by overriding the hitTest: method. I did try it but could not get it to work properly.

In the end I created a series of transparent views around the touchable object so that they did not cover it. Bit of a hack for my issue this worked fine.

Liam
A: 

According to the 'iPhone Application Programming Guide':

Turning off delivery of touch events. By default, a view receives touch events, but you can set its userInteractionEnabled property to NO to turn off delivery of events. A view also does not receive events if it’s hidden or if it’s transparent.

http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html

Updated: Removed example - reread the question...

Do you have any gesture processing on the views that may be processing the taps before the button gets it? Does the button work when you don't have the transparent view over it?

Any code samples of non-working code?

LK
Yes, I wrote in my question that the normal touches work in underneath views, but UIButtons and other elements don't work.
Sean Clark Hess
A: 

Define your subview as a subclass of UIView if it isn't already, and then implement the touchesBegan method in the subclass to pass the touches up the responder chain if the touches aren't interesting to your subview. Something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self yourMethodThatDeterminesInterestingTouches:touches withEvent:event])
        [self.nextResponder touchesBegan:touches withEvent:event]; 
}
John Stephen
Interesting. I need to dive into the responder chain more.
Sean Clark Hess