如何定义重叠MKAnnotationViews的顺序? [英] How to define the order of overlapping MKAnnotationViews?

查看:426
本文介绍了如何定义重叠MKAnnotationViews的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的地图中有几个MKAnnotations(及其相应的视图),有时会非常拥挤。现在,我的应用程序中的注释有两种形式:一些注定要保持原状,而另一些则会随着时间的推移而移动。我更喜欢在背景中看到更稳定的那些,并且移动的那些总是在它们前面传递。

I have several MKAnnotations (and their corresponding views) in my map, and it sometimes gets really crowded. Now, the annotations in my app come in two flavors: some are bound to stay where they are, while others will move as time goes on. I'd prefer to have the more stable ones visually in the background and the moving ones to always pass in front of them.

或许会想到注释最近添加到地图中的最终会到达前面(或者至少在最后面),但这似乎不是规则。据我所知,我首先创建并添加所有非移动注释,然后添加一些新实例化的移动注释,但其中许多(尽管不是全部!)最终都是在永久存货的情况下绘制的。

One would think, perhaps, that the annotations most recently added to the map would end up to the front (or alternatively at the very back, at least) but this just doesn't seem to be the rule. As far as I can tell, I create and add ALL the non-moving annotations first, and then add some newly-instantiated moving annotations, but many of them (although not all!) end up drawn under the perpetually stock-still ones.

有趣的是,当时间流逝,然而创建新的移动注释时,它们倾向于更多地倾向于顶部而不是第一个 - 即使所有移动的注释对象都是仅在非移动部件已添加到地图后才创建。

Interestingly, when time goes by, and yet new moving annotations are created, they tend to gravitate more to the top than the first ones - even if all moving annotation objects were created only after the nonmoving parts were already added to the map.

有没有人知道在地图上改变注释视图的这种奇怪的自然顺序的技巧?我试图搜索Map Kit API,但它似乎没有提到这样的事情。

Does anyone know a trick to alter this strange natural order of the annotation views on the map? I tried to search the Map Kit API, but it doesn't seem to speak of such a thing.

推荐答案

好的,所以来自MKMapViewDelegate的解决方案使用方法

Ok, so for solution use method from MKMapViewDelegate


- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
 

在此方法中,您应该在将AnnotationView添加到mapKit View后重新排列。
因此,代码可能如下所示:

In this method you should rearrange AnnotationView after it was added to mapKit View. So, code may looks like this:


- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
   for (MKAnnotationView * annView in views) {
      TopBottomAnnotation * ann = (TopBottomAnnotation *) [annView annotation];
      if ([ann top]) {
         [[annView superview] bringSubviewToFront:annView];
      } else {
         [[annView superview] sendSubviewToBack:annView];
      }
   }

}

这是有效的对我来说。

这篇关于如何定义重叠MKAnnotationViews的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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