(UIView *)hitTest问题? [英] (UIView *)hitTest problem?

查看:86
本文介绍了(UIView *)hitTest问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Masterview.it有很多childviews。我使用下面的代码来检测触摸的视图并带来相应的视图。代码工作正常。但是当我将子视图添加到子视图时,它不起作用,有什么帮助吗?

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

}

推荐答案

如果我做对了,那很简单: hitTest 总是返回视图中最远的后代子视图。如果没有子视图,则始终是相同的视图。如果有,则可能会返回该子视图。以下是解决问题的方法:

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

这篇关于(UIView *)hitTest问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆