views:

92

answers:

2
Hi, 

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 , any help please?

 - (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;
}

}

+1  A: 

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
hi, i could not move my View if i use your code?because the function i used above will return the view to move...
Mikhail Naimy
Ah, well I understand your issue. the question is not the clearest. I'll fix my answer...
Max Seelemann
See edited answer
Max Seelemann
hi, thanks works fine...but i could not click the button in Childviews?
Mikhail Naimy
A: 

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