Custom MKAnnotationView want to be tapped again
我的自定义MKAnnotationView出现问题。
我从REST api下载坐标放置位置,并使用以下代码将图钉放置在地图上:
1 2 3 4 5 6 7 8 9 10 11 12 | private func refreshMapView(andCenterMap: Bool){ DispatchQueue.main.async { [weak self] in guard let self = self else { return } self.mapView.addAnnotations(self.spotsArray) if andCenterMap { self.centerMap(at: self.mapView.userLocation.coordinate) } } } |
放置图钉的位置会自动缩放地图。
这是用于定制注释创建的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { // Do not touch the annotation of the user location if annotation.isKind(of: MKUserLocation.self){ return nil } let annoIdentifier ="SPOT" var annotationView: MKAnnotationView? if let dequeued = mapView.dequeueReusableAnnotationView(withIdentifier: annoIdentifier) { annotationView = dequeued }else{ let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annoIdentifier) annotationView = av } // Changing the image of the pin annotationView!.annotation = annotation if let image = UIImage(named:"map_pin") { annotationView!.image = image let deltaY = image.size.height/2 annotationView!.centerOffset = CGPoint(x: 0.0, y: -deltaY) }else{ annotationView!.image = nil } return annotationView } |
如您所见,我的自定义图钉正在使用此图片(@ 1x,@ 2x,@ 3x)


轻触一下,我想显示"详细视图",然后将注释用作发件人,以便在下一个视图中获得所需的所有信息。
这里的代码:
1 2 3 4 5 6 7 8 9 | func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { guard let ann = view.annotation else { return } if ann.isKind(of: MKUserLocation.self){ return } self.performSegue(withIdentifier:"showDetailSegue", sender: ann) } |
因此,我提供了所需的所有数据,然后可以返回到"地图视图"。
问题是:
在地图视图中,如果我想再次选择相同的注释,则不再可以点击。
我可以看到它(在正确的位置),但是只有当我稍微缩放一下并点击"在注释周围"时,才能再次选择它。
任何建议者如何解决这个问题?
提前致谢!
添加
1 | mapView.deselectAnnotation(ann,animated:false) |