iPhone MapView将第一个和最后一个注释置于最前面 [英] iphone mapview bring first and last annotation to the front

查看:70
本文介绍了iPhone MapView将第一个和最后一个注释置于最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些可以在mk *函数之外使用的代码.我需要运行自定义函数,以将数组中的FIRST和LAST标记置于最前面. (因此,在屏幕上所有其他标记的上方).我尝试过

I need some code which can be used outside of the mk* functions. I need to run my custom function to bring the FIRST and LAST markers in an array to the front. (so on top of all the other markers on my screen). I have tried

[self.view bringSubviewToFront:[[mapView annotations]objectAtIndex: 0]];

我已经在代码中使用了[[mapView annotations]objectAtIndex: 0],并且可以正常工作,但是当我尝试将其置于最前面时会崩溃.我访问的是错误的图层或其他内容吗?

I have used [[mapView annotations]objectAtIndex: 0] in my code and that works, but it crashes when I try to bring this to the front. Am I accessing the wrong layer or something?

感谢您的帮助.

推荐答案

您将错误的内容带到了前面.即,annotations数组是符合MKAnnotation协议(所以属于id<MKAnnotation>类型)的对象的数组,它们不是视图.

You're bringing the wrong things to the front. Namely, the annotations array is an array of objects that conform to the MKAnnotation protocol (so of type id<MKAnnotation>), and they are not views.

相反,您应该获取所需注释的视图,并将其放在最前面:

Instead, you should be getting the view for the annotations you want, and bring those to the front:

id<MKAnnotation> annotation = [[mapView annotations] objectAtIndex:0];
MKAnnotationView* annotationView = [mapView viewForAnnotation:annotation];
if (annotationView != nil) {
    [annotationView.superview bringSubviewToFront:annotationView];
}

但是,您应该注意以下几点:

However, you should note a couple of things:

    如果注释位于屏幕外的某个点上,或者注释尚未完全添加到地图上,则
  1. annotationView可能为nil.但是,如果它是nil,则可能根本不在乎屏幕上没有的注释.
  2. 您需要在annotationView的超级视图上调用bringSubviewToFront,而不是在self.view甚至是mapView上调用,因为这两个都不是annotationView的超级视图.
  1. annotationView may be nil if the annotation is on a point that's off screen, or if the annotations haven't finished being added to the map. However, if it is nil, you probably don't care if an annotation that isn't even on screen is in front or not.
  2. You need to call bringSubviewToFront on the annotationView's superview, and not on self.view or even mapView, as neither of those are the superview of the annotationView.

这篇关于iPhone MapView将第一个和最后一个注释置于最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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