Ios 如何将触摸重新映射到另一个窗口?

Ios 如何将触摸重新映射到另一个窗口?,ios,uiresponder,Ios,Uiresponder,我有一台带有外置触摸屏的iPad。我正在尝试将外部触摸屏的坐标系重新映射到此处显示的ui窗口 我正在触摸iPad上显示的ui窗口的ui视图控制器,就好像我在触摸iPad一样。我确实是用touchtype.stylus获得它们的,这就是我如何将它们与实际的iPad触摸区分开来的(我将在iPad和外部屏幕上显示不同的视图)。我正在使用以下代码: override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?

我有一台带有外置触摸屏的iPad。我正在尝试将外部触摸屏的坐标系重新映射到此处显示的
ui窗口

我正在触摸iPad上显示的
ui窗口的
ui视图控制器
,就好像我在触摸iPad一样。我确实是用touchtype
.stylus
获得它们的,这就是我如何将它们与实际的iPad触摸区分开来的(我将在iPad和外部屏幕上显示不同的视图)。我正在使用以下代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touchesOnExternalWindow = touches.filter({ $0.type == .stylus })
    let touchesOnIpad = touches.subtracting(touchesOnExternalWindow)
    if !touchesOnIpad.isEmpty {
        super.touchesBegan(touchesOnIpad, with: event)
    }

    if !touchesOnExternalWindow.isEmpty {
        guard let externalWindow = UIApplication.shared.windows.first(where: { $0.screen.bounds == screenBounds }) else {
            fatalError("Touching the external display without an external display is not supported!")
        }

        externalWindow.rootViewController?.view.touchesBegan(touchesOnExternalWindow, with: event)
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
让touchesOnExternalWindow=touchs.filter({$0.type==.stylus})
让touchesOnIpad=touchs.subtraction(touchesOnExternalWindow)
如果!touchesOnIpad.isEmpty{
super.touchsbegind(touchsonipad,with:event)
}
如果!触摸下一个TernalWindow.isEmpty{
guard let externalWindow=UIApplication.shared.windows.first(其中:{$0.screen.bounds==screenBounds})else{
fatalError(“不支持在没有外部显示器的情况下触摸外部显示器!”)
}
externalWindow.rootViewController?.view.ToucheSStart(touchesOnExternalWindow,带:事件)
}
}
我试图将触摸传递到第二个
ui窗口
,就好像我在触摸那里一样。但这似乎不起作用


如何以编程方式接触视图?我现在使用第二个屏幕上的按钮进行测试,但它也需要使用SceneKit视图。

您一次只能有一个第一响应者,并且它只能将事件传递给下一个响应者。 所以,如果你想在外部窗口中处理Stillus的触摸,你需要将此代码放入你的iPad UIViewController下一个响应程序中(还不清楚你的iPad UIViewController下一个响应程序是谁)。 在iPad UIViewController的下一个响应程序中,您输入以下代码:

override var next: UIResponder? {
    // The next responder of The UIResponder chain.
    // It will receive responder events (like touches)
    // that current responder (your iPad window UIViewController next reponder) did't handle.
    guard let externalWindow = UIApplication.shared.windows.first(where: { $0.screen.bounds == screenBounds }) else { 
       fatalError("Touching the external display without an external display is not supported!")
     }
    return externalWindow
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touchesOnExternalWindow = touches.filter({ $0.type == .stylus })
    let touchesOnIpad = touches.subtracting(touchesOnExternalWindow)
    if !touchesOnIpad.isEmpty {
        // Here you handle your touches on iPad, instead of passing it to next responder
    }

    // Pass touches to next responder(external window).
    if !touchesOnExternalWindow.isEmpty {
        super.touchesBegan(touchesOnExternalWindow, with: event)
    }
}
override var next:UIResponder?{
//UIResponder链的下一个响应程序。
//它将接收响应者事件(如触摸)
//当前响应程序(您的iPad窗口UIViewController下一个响应程序)无法处理。
guard let externalWindow=UIApplication.shared.windows.first(其中:{$0.screen.bounds==screenBounds})else{
fatalError(“不支持在没有外部显示器的情况下触摸外部显示器!”)
}
返回外部窗口
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
让touchesOnExternalWindow=touchs.filter({$0.type==.stylus})
让touchesOnIpad=touchs.subtraction(touchesOnExternalWindow)
如果!touchesOnIpad.isEmpty{
//在这里,您可以在iPad上处理您的触摸,而不是将其传递给下一个响应者
}
//将触摸传递给下一个响应者(外部窗口)。
如果!触摸下一个TernalWindow.isEmpty{
super.touchsbegind(touchsonexternalwindow,with:event)
}
}

您使用不返回任何内容的方法返回
nil
。@vrwim我为这个错误道歉。我们需要对超级用户进行转发呼叫,而不是返回nil。我正在编辑我的答案。@vrwim,我正在添加最终版本。据我所知,您希望处理除触控笔触摸事件之外的所有事件(正如您所说,您希望在iPad上绘制一些视图),并将触控笔触摸传递到外部窗口。问题是您无法将事件传递给多个下一个响应者。如果您不想处理此触摸,则需要在当前的响应程序tochesbreated方法中处理它(或者您不想以某种方式将其传递给另一个对象),并且在完成将样式触摸事件传递给外部窗口之后。