views:

506

answers:

2

How should I be handling, or rather NOT handling (ignoring), touches to my background view? It happens to be the view of my View Controller which has subviews (objects) that I DO want to respond to touch events. Setting userInteractionEnabled = NO for the view seems to turn off ALL interaction for the subviews as well.

I'm currently testing for

if ([[touch view] superview] == self.view)

in touchesBegan/Moved/Ended. But I'm trying to eliminate some conditional testing so looking for a better way...

A: 

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in frame coordinates

If you override that in your background view, you can specify which subview gets hit - you can be lazy and just ask each of your subviews if they would return yes to it, and never return yourself (return nil if they all return nil). Something like:

UIView *hitView = nil;
NSArray *subviews = [self subviews];
int subviewIndex, subviewCount = [subviews count];
for (int subviewIndex = 0; !hitView && subviewIndex < subviewCount; subviewIndex++) {
    hitView = [[subviews objectAtIndex:subviewIndex] hitTest:point withEvent:event];
}
return hitView;
Dan Keen
A: 

Please tell me how to pass the touch event to the bottom view. I have added an overlay view to the MPMoviePlayer and i am showing some subview of overlay view on touching it. I want to pass the touch event down to MPMoviePlayer's view to activate the controls at the same time.

I cant try overlayview.userInteractionEnabled = NO as i want to respond to the touch on overlay view also to show its subviews.

SKG