作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一台带外接触摸屏的 iPad。我正在尝试将外部触摸屏的坐标系重新映射到此处显示的 UIWindow
。
我正在触摸 iPad 上显示的 UIWindow
的 UIViewController
,就像我在触摸 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/
我是一名优秀的程序员,十分优秀!