【问题标题】:(UIView *)hitTest problem?(UIView *)hitTest 问题?
【发布时间】:2011-04-15 01:49:47
【问题描述】:

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

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

}

【问题讨论】:

  • 有什么原因不能直接在子视图中使用 touchesBegan: etc. 方法吗?
  • 我正在 ipad 区域周围移动 childview..所以我这样使用...当 childview 有子视图时,它不起作用..这就是问题..(当 childview 有没有子视图,它工作正常)
  • 您调用的委托方法的代码是什么?除了 [super hitTest:withEvent:] 之外,您没有将 hitView 设置为任何其他值 - 也许这很重要。
  • 对于这种命中测试,我认为您应该使用 iOS 3.2 (iPad) 中的超棒功能:developer.apple.com/library/ios/#documentation/uikit/reference/… 它工作得非常好。在我看来,使用 touchesBegan 已经没有用了。新的 UIGestureRecognizer 类确实使触摸事件变得容易。这是developer.apple.com/library/ios/#documentation/uikit/reference/… 的链接。看看吧。

标签: iphone ipad uiview uiviewcontroller


【解决方案1】:

如果我做对了,这很容易: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;

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

【讨论】:

  • 嗨,如果我使用您的代码,我无法移动我的视图?因为我上面使用的函数将返回要移动的视图...
  • 啊,我明白你的问题了。这个问题不是最清楚的。我会修正我的答案...
  • 嗨,谢谢,工作正常...但我无法单击 Childviews 中的按钮?
【解决方案2】:

正如你所说,你正在移动 UIViews。您应该查看来自WWDC 2010 的视频,他们使用新的GestureRecognizer Class 实现了这种效果。

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

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2011-08-23
    相关资源
    最近更新 更多