检测何时在 MKMapView 中选择了第二个注释 [英] Detect when a second annotation is selected in a MKMapView

查看:22
本文介绍了检测何时在 MKMapView 中选择了第二个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When a use selects an annotation in a map, I show a bottom view with informations, such as the google maps app. I'm showing it in the map's delegate :

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

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

When the user deselects it (by taping anywhere on the map), I hide my bottom view. This is done in the opposite delegate method :

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

效果很好,我很满意.但是,如果用户选择第二个注释(即:他点击第一个注释,然后点击另一个注释,同时没有先取消选择注释),我不想隐藏我的底部视图然后再次显示.我只想更改其中的信息.

It works well, I'm happy with it. However, if a user selects a second annotation (ie: he taps a first annotation, then taps another one, without deselecting first the annotation in the meantime), I don't want to hide my bottom view then show it again. I'd just like to change informations in it.

但是,由于 mapView:didDeselectAnnotationView: 被称为 before mapView:didSelectAnnotationView:,我不知道如何检测这种情况我在上面描述.

However, as mapView:didDeselectAnnotationView: is called before mapView:didSelectAnnotationView:, I can't figure out how to detect the situation I'm describing above.

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

推荐答案

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

Maybe try put a delay in your didDeselectAnnotationView method to hide your bottomView. You need to store a reference to your last selected annotation view though.

示例:

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

这篇关于检测何时在 MKMapView 中选择了第二个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆