hitTest 和子视图的问题

发布于 2024-09-24 10:23:42 字数 736 浏览 2 评论 0原文

我有一个大师视图。它有很多儿童视图。我使用以下代码来检测触摸的视图并将相应的视图置于前面。该代码运行良好。但是当我将子视图添加到子视图时,它不起作用。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-10-01 10:23:42

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

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

编辑 现在我更好地了解了问题,您也许应该执行以下操作。此代码返回超级视图,它是外部视图的直接后代:(

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

另请注意,您应该使用局部变量并稍后更改您的属性)。

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

青巷忧颜 2024-10-01 10:23:42

正如你所说,你正在移动 UIViews。您应该观看 WWDC 2010 中的视频,他们通过新的技术实现了这种效果GestureRecognizer 类

您应该查找的视频名称为“手势识别”。如果您找不到它,我可以在下班回家时将其链接到。

祝您好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文