Ios 在MKMapView中选择第二个注释时检测

Ios 在MKMapView中选择第二个注释时检测,ios,objective-c,annotations,mkmapview,Ios,Objective C,Annotations,Mkmapview,当用户在地图中选择注释时,我会显示一个包含信息的底部视图,例如谷歌地图应用程序。 我在地图的代理中显示它: - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 当用户取消选择它时(通过在地图上的任意位置单击),我隐藏了底部视图。这是通过相反的委托方法完成的: - (void)mapView:(MKMapView *)mapView didDeselectAnnotationV

当用户在地图中选择注释时,我会显示一个包含信息的底部视图,例如谷歌地图应用程序。 我在地图的代理中显示它:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
当用户取消选择它时(通过在地图上的任意位置单击),我隐藏了底部视图。这是通过相反的委托方法完成的:

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
效果很好,我很满意。但是,如果用户选择第二个注释(即:他点击第一个注释,然后点击另一个注释,同时不取消选择第一个注释),我不想隐藏底部视图,然后再次显示它。我只想更改其中的信息

但是,由于在
mapView:didSelectAnnotationView:
之前调用了,因此我不知道如何检测上面描述的情况


我的问题是:如何检测到用户选择了第二个注释,或者,我应该如何以任何其他方式解决此问题?

也许可以尝试在您的DidDecleAnnotationView方法中延迟以隐藏您的底部视图。不过,您需要存储对上次选择的注释视图的引用

例如:

@interface MyViewController
{
    MKAnnotationView *lastSelectedAnnotationView;
}

@end


@implementation MyViewController

...

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    ...

    [self updateBottomViewInfoWithAnnotationView:view];

    lastSelectedAnnotationView = view;
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    // ------------------------------------------------------------------
    // perform check to hide bottomView after a delay, to give
    // didSelectAnnotationView a chance to select new annotation
    // ------------------------------------------------------------------

    [self performSelector:@selector(checkShouldHideBottomView:) withObject:view afterDelay:0.5];
}

-(void)checkShouldHideBottomView:(MKAnnotationView *)lastDeselectedAnnotationView
{
    // ----------------------------------------------------------------------
    // Only hide bottom view if user did not select a new annotation or 
    // last selected annotation is the same as the one being deselected
    // ----------------------------------------------------------------------
    if(lastSelectedAnnotationView == nil || lastDeselectedAnnotationView == lastSelectedAnnotationView)
    {
        // hide your bottom view example
        self.bottomView.alpha = 0;

        // clear lastSelectedAnnotationView reference
        lastSelectedAnnotationView = nil;
    }
}

通过在主队列上异步执行,可以在不增加显式延迟的情况下实现这一点

 var currentSelection: MKAnnotationView?
 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
   currentSelection = view
   *** select code goes here ***
 }

 func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
   DispatchQueue.main.async { [weak self] in
     self?.delayedDeselect(view: view)
   }
 }

 func delayedDeselect(view: MKAnnotationView) {
   if currentSelection == view {
     *** deselect code goes here ***
   }
 }

我会等一等,看看是否有不那么“黑”的方法来做,除非我会尝试一下。谢谢我认为这是正确的想法,但我不确定您是否需要保留对所选注释的任何引用。在DidDecelAnnotationView中,安排隐藏方法,在didSelectAnnotationView的顶部,只需调用cancelPreviousPerformRequestsWithTarget。我最终使用了我不知道的
cancelPreviousPerformRequestsWithTarget。谢谢@DCMaxxx我遇到了类似的问题。你到底是怎么解决这个问题的?在我的例子中,我希望它同时忽略didselect和didselect。只是一个问题,使用注释数组并在didselect和didselect中管理它们怎么样?并让您的“currentAnnotation”知道您正在显示什么?您还可以通过检查
self.mapView.selectedAnnotations.count==0