我有一个 View ,里面有一张 map 。我想实现以下目标:当用户点击 map 时显示并选择注释(因此显示标注气泡)直到用户点击 map 上的另一个点(在这种情况下,应该删除之前的注释).
问题是,当我点击 map 时,注释会被添加并选中一段时间,然后不知何故它被取消选中(因此标注消失)。我希望标注气泡持续存在,直到用户点击另一个点
下面是我的尝试,请问有没有人能帮帮我?
@objc func handleTap(_ sender: UITapGestureRecognizer){
//...some code that computes the country code starting from latitude and longitude
var countryCurrency = ""
func countryRequest(countryLabel: String, completion: @escaping (String)->()){
//api request that receives the currency name of the country
completion(countryCurrency)
}
countryRequest(countryLabel: countryLabel){ countryCurrency in
DispatchQueue.main.async {
let locat:CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationCoord.latitude, locationCoord.longitude)
let annotation = MKPointAnnotation()
annotation.coordinate = locat
annotation.title = countryCurrency
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
}
我还有一个 MKMapViewDelegate 的扩展来自定义标注:
extension MapsItem: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKPointAnnotation){
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "demo")
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "demo")
annotationView!.canShowCallout = true
annotationView!.sizeToFit()
}
else{
annotationView!.annotation = annotation
}
return annotationView
}
}
提前致谢!!
最佳答案
试试这个
在添加注释之前将 isTappingNow 设置为 true,在选择它之后设置为 false
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView)
{
if(!isTappingNow)
{
self.mapView.selectAnnotation(view.annotation, animated: false)
}
}
关于ios - 如何防止自动快速取消选择注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48033517/