【问题标题】:Ignore touches to background (super) view忽略对背景(超级)视图的触摸
【发布时间】:2010-11-09 04:56:40
【问题描述】:

我应该如何处理或不处理(忽略)对我的背景视图的触摸?它恰好是我的视图控制器的视图,它具有我想要响应触摸事件的子视图(对象)。为视图设置 userInteractionEnabled = NO 似乎也关闭了子视图的所有交互。

我目前正在测试

if ([[touch view] superview] == self.view) 

在 touchesBegan/Moved/Ended 中。但我正在尝试消除一些条件测试,以便寻找更好的方法......

【问题讨论】:

    标签: iphone cocoa-touch uiview uiviewcontroller


    【解决方案1】:

    感谢丹的回答,这个问题也很好。

    但是接受的答案有一个错误:hitTesting 子视图应该在点转换为子视图的情况下完成。 此外,在“for”之前已经定义了 subviewIndex。

    由于子视图是根据它们的 z 索引排序的,因此迭代应该从最后一个到第一个(参见 Event handling for iOS - how hitTest:withEvent: and pointInside:withEvent: are related? 和 How to get UIView hierarchy index ??? (i.e. the depth in between the other subviews))。

    这是更新后的代码:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        UIView *hitView = nil;
        NSArray *subviews = [self subviews];
        int subviewIndex, subviewCount = [subviews count];
        for (subviewIndex = subviewCount-1; !hitView && subviewIndex >= 0; subviewIndex--) {
            UIView *subview = [subviews objectAtIndex:subviewIndex];
            hitView = [subview hitTest:[self convertPoint:point toView:subview] withEvent:event];
        }
        return hitView;
    }
    

    【讨论】:

      【解决方案2】:

      - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 递归调用-pointInside:withEvent:。点在框架坐标中

      如果你在你的背景视图中覆盖它,你可以指定哪个子视图被击中 - 你可以偷懒,只是询问你的每个子视图是否会返回是,并且永远不要返回自己(如果它们都返回,则返回 nil零)。比如:

      UIView *hitView = nil;
      NSArray *subviews = [self subviews];
      int subviewIndex, subviewCount = [subviews count];
      for (int subviewIndex = 0; !hitView && subviewIndex < subviewCount; subviewIndex++) {
          hitView = [[subviews objectAtIndex:subviewIndex] hitTest:point withEvent:event];
      }
      return hitView;
      

      【讨论】:

        猜你喜欢
        • 2017-05-25
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-12
        • 1970-01-01
        相关资源
        最近更新 更多