【问题标题】:Avoid flicker when moving annotations in a MKMapView on iPhone在 iPhone 上的 MKMapView 中移动注释时避免闪烁
【发布时间】:2010-08-18 16:32:39
【问题描述】:

据我所知,没有办法在不移除注释并将其重新添加到 MapView 的情况下移动注释(也许我错了)。

有没有办法防止在删除和重新添加注释之间重新绘制 MapView?现在删除注释后重新绘制会导致没有注释的框架,因此它看起来会闪烁。

我需要一个适用于 iOS 3.1 更新的解决方案。

谢谢

【问题讨论】:

    标签: iphone mkmapview


    【解决方案1】:

    在 iOS4 中

    [theAnnotation setCoordinate:newCoordinate];
    

    【讨论】:

    • 谢谢!看起来不错。我真的需要一个适用于 iOS 3.1 的解决方案。我已经编辑了问题以澄清这一点。
    • 对于那些对其工作原理感兴趣的人:Map Kit 在很大程度上依赖于键值观察。 MapView 观察其注解的标题、副标题和坐标,并在属性更改时从其委托请求新视图。当您更改这些属性时,注释会发送通知。您也可以使用[annotation willChangeValueForKey:@"coordinate"];[annotation didChangeValueForKey:@"coordinate"]; 手动发送它们。有关 KVO 的更多信息,请参阅the docu
    【解决方案2】:

    我想出了如何使用以下代码实现无闪烁注释“更新”。简而言之,您首先添加新引脚,然后删除旧引脚。我还没有简化我的代码,但你会明白的。

    -(void)replacePin:(NSString *)title SubTitle:(NSString *)subTitle Location:(CLLocationCoordinate2D)location PinType:(MapAnnotationType)pinType Loading:(BOOL)loading Progress:(float)progress
    {
        //Find and "decommission" the old pin... (basically flags it for deletion later)
        for (MyMapAnnotation *annotation in map.annotations) 
        {
            if (![annotation isKindOfClass:[MKUserLocation class]])
            {
                if ([annotation.title isEqualToString:title])
                    annotation.decommissioned = YES;
            }
        }
    
        //add the new pin...
        MyMapAnnotation *stationPin = nil;
        stationPin = [[MyMapAnnotation alloc] initWithCoordinate:Location
                                                  annotationType:pinType
                                                           title:title
                                                        subtitle:subTitle
                                                         loading:loading
                                                  decommissioned:NO
                                                        progress:progress];
        [map addAnnotation:stationPin];
        [stationPin release];
    }
    

    然后,我拨打电话搜索并删除所有退役的引脚:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        MKAnnotationView* annotationView = nil;
    
        if (![annotation isKindOfClass:[MKUserLocation class]])
        {
            MyMapAnnotation* annotation = (MyMapAnnotation*)annotation;
    
            //your own details...
    
            //delete any decommissioned pins...
            [self performSelectorOnMainThread:@selector(deletePin:) withObject:annotation.title waitUntilDone:NO];
        }
        return annotationView;
    
    }
    

    最后,在后台摆脱旧别针:

    -(void)deletePin:(NSString *)stationCode
    {
        for (MyMapAnnotation *annotation in map.annotations) 
        {
            if (![annotation isKindOfClass:[MKUserLocation class]])
            {
                if ([annotation.title isEqualToString:stationCode])
                {  
                    if (annotation.decommissioned)
                        [map removeAnnotation:annotation];
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      对于 3.1,您必须破解。我会看到两种可能性,要么尝试通过潜伏在头文件中并直接访问内存来强制更改当前注释的坐标,要么摆弄 UIWindow 和图形上下文以尝试避免在下一个 runloop 循环中完成绘图实际到达屏幕。

      编辑:第三种选择,这可能是最简单的,是在 MapKit 之外进行自己的轻量级注释实现..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多