【发布时间】: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)