swift - 名称节点始终为零 - ARKit Swift

标签 swift scenekit arkit

我正在尝试接收与通过“hitTest”点击的节点关联的名称节点,但每次我点击一个节点时,名称都是 nil。

这是我创建节点并将名称关联到它们的函数:

private func findLocalPlaces(topics: [Topic]) {

    guard let location = self.locationManager.location else {
        return
    }

    for topic in topics {
        let topicLocation = CLLocationCoordinate2D(latitude: topic.coordinates!.x, longitude: topic.coordinates!.y)
        let locationPin = CLLocation(coordinate: topicLocation, altitude: location.altitude)
        //Set the image of the pin
        let urlImage = topic.image?.image(topicId: topic._id, imageType: .mobileHeader)
        Alamofire.request(urlImage ?? "").responseImage { response in
            if let image = response.result.value {

                //self.annotationNode = LocationAnnotationNode(location: locationPin, image: image)

                //Create a view instead of the only image view
                let viewDemo = UIView()
                viewDemo.frame = CGRect(x: 0, y: 0, width: 500, height: 281)

                let imageView = UIImageView(image: image)
                imageView.frame = CGRect(origin: .zero, size: viewDemo.frame.size)
                viewDemo.addSubview(imageView)

                let label = UILabel()
                label.textColor = .white
                label.frame = CGRect(x: 10, y: 200, width: 400, height: 30)
                label.text = topic.name
                viewDemo.addSubview(label)

                self.annotationNode.name = topic.name


                self.annotationNode = LocationAnnotationNode(location: locationPin, view: viewDemo)


                //Scale the POI based on the distance
                self.annotationNode.scaleRelativeToDistance = false

                DispatchQueue.main.async {
                    //Add the POI to the scene
                    self.sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: self.annotationNode)
                }
            }
        }
    }
}

annotationNode初始化到最顶层为:

var annotationNode: LocationAnnotationNode = LocationAnnotationNode(location: CLLocation(latitude: 0, longitude: 0), image: UIImage(named:"arPin")!)

编辑

HitTest 相关代码:

 //Method called when tap
@objc func handleTap(rec: UITapGestureRecognizer){

    if rec.state == .ended {
        let location: CGPoint = rec.location(in: sceneLocationView)
        let hits = self.sceneLocationView.hitTest(location, options: nil)
        if !hits.isEmpty{

            let tappedNode = hits.first?.node
            print("NODE TAPPED", tappedNode?.name)
        }
    }
}

最佳答案

您要为节点分配一个名称,然后用一个没有名称的新节点覆盖整个节点?

您在 if let 之后在顶部有一个注释行,然后您将名称设置为 self.annotationNode 然后覆盖它,导致命名无效。删除第二个: self.annotationNode = LocationAnnotationNode(location: locationPin, view: viewDemo) 并取消注释第一个

关于swift - 名称节点始终为零 - ARKit Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965974/

相关文章:

ios - 是否可以在 React Native 中使用 iOS 13 系统字体?

swift - 如何在Scenekit-iOS中检测两个不同.scn文件的节点之间的碰撞?

swift - 一个枚举可以包含 Swift 中的另一个枚举值吗?

ios - 无法重新加载 TableView

swift - 如何在 ARKit 3.0 中启用 `Depth of Field` 选项?

swift - ARKit 检测平面之间的相交

ios - 使用 SceneKit 在 ARKit 中拖动 SCNNode

swift - 有没有办法将 GLB 文件下载到 ArKit/RealityKit 应用程序中?

swift - Firestore : How to set security on object types with users as field names

SceneKit 布料物理 - 有可用的解算器吗?