(UIView *)hitTest problem?

2019-06-14 12:18发布

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

}

2条回答
唯我独甜
2楼-- · 2019-06-14 12:29

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).

查看更多
贪生不怕死
3楼-- · 2019-06-14 12:30

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!

查看更多
登录 后发表回答