gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:26:23 26 4
gpt4 key购买 nike

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

我正在触摸 iPad 上显示的 UIWindowUIViewController,就像我在触摸 iPad 一样。我确实使用触摸类型 .stylus 获取它们,这就是我如何将它们与实际的 iPad 触摸区分开来(我将在 iPad 和外部屏幕上显示不同的 View )。我正在使用以下代码:

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,就好像我在那里触摸一样。但这似乎不起作用。

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

最佳答案

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

关于ios - 如何将触摸重新映射到另一个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58431984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com