在堆叠的UIWindows之间传递触摸? [英] Passing touches between stacked UIWindows?

查看:52
本文介绍了在堆叠的UIWindows之间传递触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个窗口-前一个和后一个.前一个用来在后一个之上叠加一些内容.我希望能够捕获前窗的某些部分的触摸,但不能捕捉其他部分的触摸;我希望前窗口在某些区域能够接收到触摸,但在其他区域将它们传递到后窗口.

I have two windows -- a front one, and a back one. The front one is used to overlay some stuff on top of the back one. I want to be able to capture touches on some parts of the front window, but not others; I want the front window to receive touches in some areas, but pass them through to the back window in others.

关于我将如何做的任何想法?

Any ideas as to how I'd do this?

推荐答案

好,这是我做的:我在前窗口中创建了两个视图.第一个视图覆盖了我想要捕捉的区域;第二个是我希望触摸通过的地方.我将UIWindow子类化,并覆盖了hitTest:withEvent方法,如下所示:

Ok, here's what I did: I created two views in the front window. The first view covered the area where I wanted to catch the touches; the second, where I wanted the touches to pass through. I sub-classed UIWindow and overrode the hitTest:withEvent method like so:

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

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

And in the class which creates the front window, I used a gesture recognizer to catch touches on the first view.

与2017年的代码相同:

Same in 2017 code:

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

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

Your "uber" window will be some view controller, UberVc.

只需将UberVc的主视图(即简单的背景.view)设置为PassView.

Just make the main view (that is to say, simply the background .view) of UberVc be a PassView.

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

Then add say buttons etc. on the UberVc.

上面的代码导致对UberVc的按钮的任何单击,而对按钮(即背景")上的任何单击的通向常规窗口/VC的访问.

The above code results in any clicks on the buttons going to UberVc, and any clicks not on the buttons (ie, on the "background") going through to your regular window/VCs.

这篇关于在堆叠的UIWindows之间传递触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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