bts*_*umy 13 iphone mapkit mkpinannotationview
我对MKMapKit的局限性感到相当沮丧.我当前的问题与注释视图的z排序有关,特别是当它与触摸相关时.如果您接受默认的z顺序,mapkit会为您提供:
我尝试使用与Web上的以下代码类似的东西来解决#1(其设计旨在对z顺序进行一些控制)
- (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都有相同的superview(私有类MKOverlayView).您会认为任何合理的设计都会绘制有关此叠加视图的标注.
有没有人成功解决了#1或#2?
如果你有一个自定义annotationView(听起来你已经有),你可以尝试添加这个:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview bringSubviewToFront:self];
[super touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
副作用是每次触摸标记时它也会弹出到z顺序的前面,这实际上是明智的,因为这是用户关注的内容.
拉明,这对我想做的事情来说是完美的.我没有定义自定义注释视图.我通过使用一个类别解决了这个问题,它完美无缺!
这是标题:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MKPinAnnotationView (ZIndexFix)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end
Run Code Online (Sandbox Code Playgroud)
和实施:
#import "AEBMapViewCategory.h"
@implementation MKPinAnnotationView (ZIndexFix)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview bringSubviewToFront:self];
[super touchesBegan:touches withEvent:event];
}
@end
Run Code Online (Sandbox Code Playgroud)
尝试另一种解决方案,在以下位置设置注释视图图层的zPosition(annotationView.layer.zPosition):
(void)mapView:(MKMapView*)mapView didAddAnnotationViews:(NSArray*)views;
归档时间: |
|
查看次数: |
8615 次 |
最近记录: |