【问题标题】:Detect touch on child node of object in SceneKit在 SceneKit 中检测对象的子节点上的触摸
【发布时间】:2019-01-10 09:01:20
【问题描述】:

基本上是this question,但对于 SceneKit。

我有一个父节点,里面有几个较小的节点,稍后的父节点变得透明,(父节点的漫反射材质不透明度设置为 0)之后我想获得在里面点击的节点对象,我应该怎么做呢?默认命中测试返回父节点,由于对象内部有几个较小的节点,所以我需要精确的被点击的节点。

【问题讨论】:

    标签: swift scenekit


    【解决方案1】:

    要解决此问题,我建议阅读 Apple 的下一个主题:

    https://developer.apple.com/documentation/scenekit/scnhittestoption

    总体思路:

    func registerGestureRecognizer() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(search))
        self.sceneView.addGestureRecognizer(tap)
    }
    
    @objc func search(sender: UITapGestureRecognizer) {
    
        let sceneView = sender.view as! ARSCNView
        let location = sender.location(in: sceneView)
        let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1])
    
        guard sender.state == .ended else { return }
    
            for result in results.filter( { $0.node.name == "Your node name" }) {
                // do manipulations
        }
    }
    

    希望对您有所帮助! 最好的问候。

    【讨论】:

    • 感谢您抽出宝贵时间回答,根据您调查命中测试选项的建议,我发现获得所需结果的最佳方法是使用 categoryBitMask 选项,因为我可以分配所有较小的节点对象内部到特定类别,命中测试可以适当地返回父对象内部的节点。
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2016-01-12
    • 2018-09-20
    相关资源
    最近更新 更多