Selecting a MapView Annotation Twice

风格不统一 提交于 2019-12-12 10:58:58

问题


My iPhone app has a mapview with a large number of locations that the user can select from. I would like him to be able to tap on one of the annotations to display its callout view, and then again to actually select it. The problem is that the didSelectAnnotationView only gets called once.

So how can I detect the selection an already selected annotation? Alternatively, how can I deselect an annotation without hiding the callout view? The user can work round this by deselecting the annotation before he selects it again, but this is not intuitive, and I want to avoid him having to do this.


回答1:


I just found the solution with the code below:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    [mapview deselectAnnotation:view.annotation animated:NO];

}

This way the selected annotation gets deselected and you can select it once again.




回答2:


For those who are having this problem in '16, here is the Swift version:

mapView.deselectAnnotation(view.annotation!, animated: false)



回答3:


The annotation can't move from the selected state to the selected state again.

You can instead use a custom mkannotationview, where you override setSelected, and install a UITapGestureRdcognizer on your view, and remove it when the view is deselected.

wire up the recognized to do what you want for the tap-when-selected state.



来源:https://stackoverflow.com/questions/9874255/selecting-a-mapview-annotation-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!