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