【问题标题】:Swift SCNNode subclass hittest always returns SCNNode *not* subclassSwift SCNNode 子类 hittest 总是返回 SCNNode *not* 子类
【发布时间】:2017-10-03 04:24:54
【问题描述】:

我有一个 SCNNode 的子类“ExSCNNode”来为 SCNNode 添加更多属性和行为。

class ExSCNNode : SCNNode {
...
}

我比用 ExSCNNode 构建一个场景。

let testnode = ExSCNNode()

在测试场景时:

// check what nodes are tapped
let p = gestureRecognize.location(in: scnView)
let hitResults = scnView.hitTest(p, options: [:])

// check that we clicked on at least one object
if hitResults.count > 0 {

for hit in hitResults {
let hitnode = hit.node
...

hitnode 是 SCNNode 而不是 ExSCNNode。 但我想让 ExSCNNode 访问高级功能。

如何访问子类而不是 SCNNode 类?

【问题讨论】:

  • 您需要将其创建为ExSCNNode。您尚未包含创建它的代码。
  • 我创建了一个 ExSCNodes 场景。上例中只添加了一个

标签: swift scenekit


【解决方案1】:

只需将对象转换为您的子类:

// check what nodes are tapped
let p = gestureRecognize.location(in: scnView)
let hitResults = scnView.hitTest(p, options: [:])

for hit in hitResults {
    if let hitnode = hit.node as? ExSCNNode {

        …
    }

【讨论】:

  • 扩展不允许直接存储属性
  • 这不起作用你我无法将被点击的节点转换为自定义节点类型!......
  • @BenSmith 命中测试将始终返回包含几何图形的节点,因此请确保您的自定义子类是附加几何图形的节点
猜你喜欢
  • 2015-06-19
  • 2023-03-17
  • 2015-01-04
  • 2017-05-16
  • 2018-02-22
  • 2018-10-14
  • 2017-06-28
  • 1970-01-01
  • 2011-05-11
相关资源
最近更新 更多