iPhone MapView将第一个和最后一个注释置于最前面 [英] iphone mapview bring first and last annotation to the front
问题描述
我需要一些可以在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:
-
如果注释位于屏幕外的某个点上,或者注释尚未完全添加到地图上,则
-
annotationView
可能为nil
.但是,如果它是nil
,则可能根本不在乎屏幕上没有的注释. - 您需要在
annotationView
的超级视图上调用bringSubviewToFront
,而不是在self.view
甚至是mapView
上调用,因为这两个都不是annotationView
的超级视图.
annotationView
may benil
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 isnil
, you probably don't care if an annotation that isn't even on screen is in front or not.- You need to call
bringSubviewToFront
on theannotationView
's superview, and not onself.view
or evenmapView
, as neither of those are the superview of theannotationView
.
这篇关于iPhone MapView将第一个和最后一个注释置于最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!