【发布时间】:2010-08-18 16:32:39
【问题描述】:
据我所知,没有办法在不移除注释并将其重新添加到 MapView 的情况下移动注释(也许我错了)。
有没有办法防止在删除和重新添加注释之间重新绘制 MapView?现在删除注释后重新绘制会导致没有注释的框架,因此它看起来会闪烁。
我需要一个适用于 iOS 3.1 更新的解决方案。
谢谢
【问题讨论】:
据我所知,没有办法在不移除注释并将其重新添加到 MapView 的情况下移动注释(也许我错了)。
有没有办法防止在删除和重新添加注释之间重新绘制 MapView?现在删除注释后重新绘制会导致没有注释的框架,因此它看起来会闪烁。
我需要一个适用于 iOS 3.1 更新的解决方案。
谢谢
【问题讨论】:
在 iOS4 中
[theAnnotation setCoordinate:newCoordinate];
【讨论】:
[annotation willChangeValueForKey:@"coordinate"]; 和[annotation didChangeValueForKey:@"coordinate"]; 手动发送它们。有关 KVO 的更多信息,请参阅the docu
我想出了如何使用以下代码实现无闪烁注释“更新”。简而言之,您首先添加新引脚,然后删除旧引脚。我还没有简化我的代码,但你会明白的。
-(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.1,您必须破解。我会看到两种可能性,要么尝试通过潜伏在头文件中并直接访问内存来强制更改当前注释的坐标,要么摆弄 UIWindow 和图形上下文以尝试避免在下一个 runloop 循环中完成绘图实际到达屏幕。
编辑:第三种选择,这可能是最简单的,是在 MapKit 之外进行自己的轻量级注释实现..
【讨论】: