【问题标题】:Name node always nil - ARKit Swift名称节点始终为零 - ARKit Swift
【发布时间】:2019-09-21 18:34:35
【问题描述】:

我正在尝试接收与通过“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)
        }
    }
}

【问题讨论】:

  • 你能分享你执行hitTest的代码吗?
  • 确定我正在更新问题
  • 在代码中的哪个位置将手势识别器添加到视图中?
  • 在viewDidLoad中为:let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(rec:))) sceneLocationView.addGestureRecognizer(tap)
  • 呃,我是否遗漏了什么,或者您是否为节点分配了一个名称,然后用一个没有名称的新节点覆盖整个节点? self.annotationNode.name = topic.name 然后self.annotationNode = LocationAnnotationNode(location: locationPin, view: viewDemo)

标签: swift scenekit arkit


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    @objc func handleTap(rec: UITapGestureRecognizer){
    
        if rec.state == .ended {
            let location: CGPoint = rec.location(in: sceneLocationView)
            let hits = self.sceneLocationView.hitTest(location, options: nil).filter { $0.node.name != nil }
            if !hits.isEmpty{
    
                let tappedNode = hits.first?.node
                print("NODE TAPPED", tappedNode?.name)
            }
        }
    }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 2020-01-07
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多