Reputation: 837
I'm trying to receive the name node associated to the node tapped through "hitTest" but every time that I'm doing tap on a node, the name is nil.
This is the function where I'm creating the nodes and associating the name to them:
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)
}
}
}
}
}
The annotationNode is initialized to the top as:
var annotationNode: LocationAnnotationNode = LocationAnnotationNode(location: CLLocation(latitude: 0, longitude: 0), image: UIImage(named:"arPin")!)
EDIT
Code related to the hit test:
//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)
}
}
}
Upvotes: 0
Views: 188
Reputation: 1915
You are assigning a name to the node, and then override the whole node with a new node without a name?
You have a commented line at top after your if let
and then you set name to self.annotationNode
and then you override it, causing the naming to be useless. Remove the second:
self.annotationNode = LocationAnnotationNode(location: locationPin, view: viewDemo)
and uncomment the first one
Upvotes: 1
Reputation: 1068
You must be sure that your hits doesn't contain nil objects.
@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)
}
}
}
Hope it helps!
Upvotes: 1