本文介绍了MapKit(MKMapView):zPosition在iOS11上不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS11上,zPosition停止为注释视图.图层工作.每次地图区域更改.

On iOS11 the zPosition stopped working for the annotationView.layer. Every time the map region changes.

  • 原始解决方案没有运气:layer.zPosition = X;
  • bringViewToFront/SendViewToBack方法没有运气
  • No luck with original solution: layer.zPosition = X;
  • No luck with bringViewToFront/SendViewToBack methods

Xcode 8.3/9

更新(感谢Elias Aalto):

创建MKAnnotationView时:

When creating MKAnnotationView:

annotationView.layer.zPosition = 50;
if (IS_OS_11_OR_LATER) {
    annotationView.layer.name = @"50";
    [annotationView.layer addObserver:MeLikeSingleton forKeyPath:@"zPosition" options:0 context:NULL];

}

在MeLikeSingleton或您在其中存在的任何观察者对象中:

In MeLikeSingleton or whatever observer object you have there:

- (void)observeValueForKeyPath:(NSString *)keyPath
                         ofObject:(id)object
                           change:(NSDictionary *)change
                          context:(void *)context {

       if (IS_OS_11_OR_LATER) {

           if ([keyPath isEqualToString:@"zPosition"]) {
               CALayer *layer = object;
               int zPosition = FLT_MAX;
               if (layer.name) {
                   zPosition = layer.name.intValue;
               }
               layer.zPosition = zPosition;
               //DDLogInfo(@"Name:%@",layer.name);
           }

       }
}

  • 此解决方案使用layer.name值来跟踪zOrder.如果您有许多级别的zPosition(用户位置,群集,引脚,标注);)
  • 没有for循环,只有KVO
  • 我使用了Singleton Object来观察图层值的变化.如果您在整个应用程序中使用了多个MKMapViews.
  • 在IOS11之前如何工作

    .. is使用

    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
    

    并在此处设置 zPosition .

    ..但是那(对于我们中的某些人,仍然不知道为什么)在iOS11中不再起作用!

    ..but that (for some of us, still dunny why) does not work anymore in iOS11!

    推荐答案

    zPosition可以正常工作,只是MKMapView根据有些破损和无用的MKFeatureDisplayPriority在内部覆盖了它.如果您只需要少量注释才能保留在其他所有内容"之上,则可以使用KVO干净地完成 semi .只需在注释视图的图层的zPosition中添加一个观察者,然后覆盖它即可,因为MKMapView试图对其进行摆弄.

    The zPosition does work, it's just that MKMapView overwrites it internally based on the somewhat broken and useless MKFeatureDisplayPriority. If you just need a handful of annotations to persist on top of "everything else", you can do this semi cleanly by using KVO. Just add an observer to the annotation view's layer's zPosition and overwrite it as MKMapView tries to fiddle with it.

    (请原谅我的ObjC)

    (Please excuse my ObjC)

    添加观察者:

            [self.annotationView.layer addObserver:self forKeyPath:@"zPosition" options:0 context:nil];
    

    否决MKMapView

    Overrule MKMapView

     - (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
    {
        if(object == self.annotationView.layer)
        {
            self.annotationView.layer.zPosition = FLT_MAX;
        }
    }
    

    利润

    这篇关于MapKit(MKMapView):zPosition在iOS11上不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 14:40