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

当用户在地图中选择注释时,我会显示带有信息的底部视图,例如google地图应用.
我在地图的代表中显示它:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

当用户取消选择它(通过在地图上的任何位置录制)时,我隐藏了我的底部视图.这是在相反的委托方法中完成的:

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

它运作良好,我很高兴.但是,如果用户选择第二个注释(即:他点击第一个注释,然后点击另一个注释,同时不首先取消选择注释),我不想隐藏我的底部视图然后再次显示它.我只想改变它的信息.

但是,由于mapView:didDeselectAnnotationView:在mapView:didSelectAnnotationView:之前调用,我无法弄清楚如何检测我上面描述的情况.

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

解决方法

也许尝试在didDeselectAnnotationView方法中加一个延迟来隐藏你的bottomView.您需要存储对上次选择的注释视图的引用.

例:

@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;
    }
}

以上是来客网为你收集整理的ios – 在MKMapView中选择第二个注释时检测全部内容,希望文章能够帮你解决ios – 在MKMapView中选择第二个注释时检测所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。