MKAnnotationViews的Z顺序 [英] Z-ordering of MKAnnotationViews

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

问题描述

我对MKMapKit的局限感到非常沮丧.我当前的问题与注释视图的Z顺序有关,尤其是与触摸有关的问题.如果您接受默认的z顺序,则地图包会为您提供:

I'm getting fairly frustrated with the limitations of MKMapKit. My current problem has to do with the z-ordering of annotation views, particularly as it relates to touches. If you accept the default z-order the mapkit gives you:

  1. 该订单随机出现. z顺序与添加注释的顺序无关.
  2. 如果一个注释结束于另一个注释的顶部,则触摸顶部注释通常会调出底部注释的标注.似乎点击检测甚至不遵守抽签顺序.这是怎么回事?

我尝试使用类似于以下在网络上找到的代码(旨在对z顺序进行一些控制)来解决#1问题

I've tried solving #1 using something similar to the following code found on the web (which was designed to give some control over z-order)


- (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];
      }
   }

}

遍历传递给mapView:didAddAnnotationViews的注释视图并调整其z顺序似乎可以解决#1.问题在于,现在标注视图不再总是位于注释视图的顶部. MapKit似乎对图层感到非常困惑(标注应绘制在所有注释视图上方的图层中).我什至可以弄清楚它是如何变得混乱的,因为您收到的所有MKAnnotationViews都具有相同的超级视图(私有类MKOverlayView).您会认为任何合理的设计都会绘制有关此叠加视图的标注.

Running through the annotation views passed to mapView:didAddAnnotationViews: and adjusting their z-order does seem to fix #1. The problem is that now the callout views are no longer always on top to the annotation views. The MapKit seems to get very confused about the layers (callouts should be drawn in a layer above all annotation views). I can even figure out how it is getting confused since all the MKAnnotationViews you receive have the same superview (a private class MKOverlayView). You would think any reasonable design would draw the callouts about this overlay view.

有人成功解决了#1或#2吗?

Has anyone solved #1 or #2 successfully?

推荐答案

如果您有一个自定义的注解视图(听起来已经拥有),则可以尝试添加以下内容:

If you have a custom annotationView (which sounds like you already have) you can try adding this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [self.superview bringSubviewToFront:self];
    [super touchesBegan:touches withEvent:event];
}

副作用是,每次触摸标记时,标记也会弹出到z顺序的前面,这实际上是明智的,因为这正是用户关注的重点.

A side-effect is that each time a marker is touched it also pops to the front of the z-order, which actually is sensible since that's what the user is focusing on.

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

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