问题描述
我正在尝试在用户单击特定的SCNNode之后检索鼠标位置.我最终希望执行一次命中测试,以确定单击了哪个SCNNode.我有以下代码,但是一旦运行项目,我就会得到一个断言错误,指出:无法获得locationInView:对于这种类型的NSTouch".任何帮助将不胜感激.
I'm trying to retrieve the mouse location after a user clicks on a specific SCNNode. I eventually want to perform a hit test to determine which SCNNode was clicked. I have the following code but once I run the project I get an assertion error stating: "Cannot get locationInView: for this type of NSTouch". Any help would be appreciated.
override func touchesBegan(with event: NSEvent) {
let touch = event.touches(for: self.view)
if #available(OSX 10.12.2, *) {
let location = touch.first?.location(in: self.view)
let hitResults = sceneView.hitTest(location!, options: nil)
} else {
// Fallback on earlier versions
}
}
推荐答案
-locationInView:
仅适用于 type
NSTouchTypeDirect
.如果您的Mac带有触摸栏,则按下它会导致触摸类型为 NSTouchTypeIndirect
,它在屏幕上没有位置.
-locationInView:
will only work for touches of type
NSTouchTypeDirect
. If you have a Mac with a Touch Bar, pressing it will lead to an event with a touch of type NSTouchTypeIndirect
which has no location on the screen.
这篇关于无法获取locationInView:尝试检索位置时出现此类型的NSTouch断言错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!