0

I have one Masterview. It has lot of childviews. I am using the following code to detect the touched view and to bring front the corresponding view. The code works fine. But when I add subview to childview, it did not work.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    self.hitView = nil;
    self.hitView = [super  hitTest:point withEvent:event];
    int x =  self.hitView.frame.origin.x;
    int y =  self.hitView.frame.origin.y;
    NSLog(@"x = %d",x);
    NSLog(@"y = %d",y);
    if ([self.viewDelegate respondsToSelector:
             @selector(view:hitTest:withEvent:hitView:)])
    {
        return [self.viewDelegate view:self hitTest:point
                                 withEvent:event hitView:hitView];
    }
    else
    {
        
           [self bringSubviewToFront:self.hitView];
        return hitView;
    }
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • Is there a reason why you can't your the touchesBegan: etc. methods in your subviews directly? – Eiko Sep 21 '10 at 08:12
  • What's the code for that delegate method you call? You don't set the hitView to anything else than [super hitTest:withEvent:] - maybe this matters. – Eiko Sep 21 '10 at 08:36
  • For this kind of hit test I think you should use the awesome feature in iOS 3.2 (iPad): http://developer.apple.com/library/ios/#documentation/uikit/reference/UITapGestureRecognizer_Class/ It works really well. There is no use anymore to use touchesBegan, in my opinion. The new UIGestureRecognizer Classes really makes touch events easy. Here is a link to the http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html. Check it out. – hellozimi Sep 21 '10 at 09:39

2 Answers2

7

If I get it right, it's pretty easy: hitTest always returns the farthest descendant subview in the view. If there is no subview this is always the same view. If there is one, that subview might be returned instead. Here's how you could fix it:

self.hitView = [super hitTest:point withEvent:event];
if ([self.hitView isDescendantOfView: self])
  self.hitView = self;

EDIT Now that I understand the problem better, you should maybe do the following. This code returns the superview that's a direct descendant of the outer view:

UIView *hitView = [super hitTest:point withEvent:event];
while (hitView && hitView.superview != self)
  hitView = hitView.superview;

(Please also note that you should use a local variable and change your property later than).

Max Seelemann
  • 9,344
  • 4
  • 34
  • 40
0

As you said you're moving UIViews around. You should check out the video from WWDC 2010 where they achieve this effect with the new GestureRecognizer Class.

The video you should look for is named somthing with "Gesture Recognition". If you don't find it I can link it when I'm home from work.

I wish you the best of luck!

hellozimi
  • 1,858
  • 19
  • 21