Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

iphone - Z-ordering of MKAnnotationViews

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. The order appears random. The z-order is unrelated to the order the annotations were added.
  2. If one annotation ends up on top of another one, touching the top annotation generally brings up the callout for the bottom annotation. It seems like the hit detecting doesn't even respect the draw order. What's up with that?

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

}

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.

Has anyone solved #1 or #2 successfully?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...