名称节点始终为零-ARKit Swift

时间:2019-05-03 08:27:19

标签: 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")!)

编辑

与点击测试相关的代码:

 //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)
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您必须确保 hits 不包含nil个对象。

6 in Fruit.__members__.values() 

希望有帮助!

答案 1 :(得分:1)

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

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