Я пытаюсь получить имя узла, связанного с узлом, которое прослушивается через «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)
}
}
}