在堆叠的UIWindows之间传递触摸?

Ann*_*son 25 touch uiview uiwindow ios

我有两个窗户 - 一个是前窗,另一个是后窗.前面的一个用于覆盖后面的一些东西.我希望能够捕捉前窗某些部分的触摸,而不是其他部分; 我希望前窗在某些区域接收触摸,但是将它们传递到其他区域的后窗.

关于我怎么做的任何想法?

Ann*_*son 33

好的,这就是我做的:我在前窗口创建了两个视图.第一个视图涵盖了我想要触及的区域; 第二,我希望触摸通过.我对UIWindow进行了分类并覆盖了hitTest:withEvent方法,如下所示:

- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {

  // See if the hit is anywhere in our view hierarchy
  UIView *hitTestResult = [super hitTest:point withEvent:event];

  // ABKSlideupHostOverlay view covers the pass-through touch area.  It's recognized
  // by class, here, because the window doesn't have a pointer to the actual view object.
  if ([hitTestResult isKindOfClass:[ABKSlideupHostOverlayView class]]) {

    // Returning nil means this window's hierachy doesn't handle this event. Consequently,
    // the event will be passed to the host window.
    return nil;
  }

  return hitTestResult;
}
Run Code Online (Sandbox Code Playgroud)

在创建前窗的类中,我使用手势识别器来捕捉第一个视图的触摸.


2017年代码相同:

class PassView: UIView {}

class UIHigherWindow: UIWindow {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

        let hitView = super.hitTest(point, with: event)

        if hitView!.isKind(of: PassView.self) {

            return nil
        }

        return hitView
    }
}
Run Code Online (Sandbox Code Playgroud)

你的"超级"窗口将是一些视图控制器,UberVc.

只需让UberVc的主视图(也就是说,背景.view)成为PassView.

然后在UberVc上添加说按钮等.

上面的代码会导致UberVc按钮上的任何点击,以及任何不在按钮上的点击(即,在"背景"上)点击进入常规窗口/ VC.