mapView:didDeselectAnnotationView:在注释视图实际取消选择之前调用委托方法
我正在使用填充有自定义图钉的地图视图。当用户点击地图上的某处以取消选择图钉时,我想实现地图,以便该图钉不会被取消选择(即用户无法在不选择其他图钉的情况下取消选择图钉,因此至少有一个图钉将始终被选择)。这是我对 didDeselectAnnotationView 方法的实现:
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
[mapView selectAnnotation:view.annotation animated:NO];
}
本质上,我正在尝试重新选择注释。然而,经过一些调试并打印到控制台后,我意识到注释视图实际上并没有被取消选择,直到方法 didDeselectAnnotationView: 完成运行之后(也就是说,事件的顺序是:用户点击地图上的某处,调用 didDeselectAnnotationView:并完成执行,注释视图实际上被取消选择)。有没有其他人遇到过这个问题,或者是否有人知道另一种方法来强制地图的行为,使得用户无法在不选择其他图钉的情况下取消选择图钉,从而始终选择一个图钉?
感谢您的帮助。
I am working with a map view populated with custom pins. When the user taps somewhere on the map to deselect a pin, I want to implement the map such that the pin does not become deselected (i.e. users are not able to deselect pins without selecting other pins, so at least one pin will always be selected). Here is my implementation of the didDeselectAnnotationView method:
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
[mapView selectAnnotation:view.annotation animated:NO];
}
Essentially, I'm trying to reselect the annotation. However, after some debugging and printing to console, I realized that the annotation view isn't actually being deselected until after the method didDeselectAnnotationView: finishes running (that is, the order of events goes: user taps somewhere on map, didDeselectAnnotationView: is called and finishes executing, the annotation view is actually deselected). Has anyone else experienced this problem, or does anyone know another way to enforce the behavior for the map such that users are unable to deselect pins without selecting other pins, so that one pin will always be selected?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试将重新选择推迟到
didDeselectAnnotationView
完成之后:Try deferring the re-select until after
didDeselectAnnotationView
is finished:编辑:请记住,此方法会阻止选择任何其他引脚,而创建此答案时这是未知的。很可能,这不是您想要的行为。
我知道这是一个老问题,但接受的答案在 iOS 8 中对我不起作用。对我有用的是完全禁用默认情况下包含在 MKMapView 中的 UITapGestureRecognizer。
希望这对其他人有帮助。
干杯!
EDIT: Keep in mind that this method prevents any other pins from being selected, which was unknown when this answer was created. Chances are, this is not your desired behaviour.
I know this is an old question, but the accepted answer didn't work for me in iOS 8. What worked for me was completely disabling the UITapGestureRecognizer that, by default, is included in a MKMapView.
Hopefully this helps someone else.
Cheers!
我也有类似的问题,但恰恰相反。
根据所选的引脚,表格将滚动到相应的单元格。如果未选择引脚,表格将滚动回第一个单元格。当选择另一个 pin 时,在调用 select 方法的同时调用取消选择方法,并且表格不会按要求滚动。
以下代码解决了该问题,并对 Anna 的解决方案进行了轻微修改。
I had a similar issue, but the other way around.
Depending on the pin selected a table would scroll to the respective cell. If no pin was selected the table would scroll back to the first cell. The deselect method was being called at the same time the select method was being called when another pin was selected and the table would not scroll as required.
The following code resolved the issue and is a slight modification to Anna's solution.
安娜的回答很棒。
这是 Swift 3 或 4 中的答案:D
Awesome answer from Anna.
Here the answer in Swift 3 or 4 :D
如果您为地图视图添加手势,请尝试以下操作:
返回 NO,它可以工作
if you add a gesture for map view, try this:
return NO, it works