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



我有一台带外部触摸屏的iPad。我正在尝试将外部触摸屏的坐标系重新映射到那里显示的UIWindow

我正在触摸iPad上显示的UIWindowUIViewController,就像我触摸iPad一样。我确实使用触摸类型.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)
}
}

我试图将触摸传递给第二个UIWindow,就好像我在那里触摸一样。但这似乎行不通。

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

一次只能有一个第一响应者,并且它只能将事件传递给下一个响应者。 因此,如果您想在外部窗口中处理带有stilus的触摸,则需要将此代码放入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)
}
}

最新更新