[英]MKMapView addAnnotation not adding annotation immediately
我有以下简单的代码:
self.departureAnnotation = [[UserAnnotation alloc] init];
self.departureAnnotation.coordinate = self.map.centerCoordinate;
[self.map addAnnotation:self.departureAnnotation];
[self.map selectAnnotation:self.departureAnnotation animated:YES];
这段代码应该做的(显然)是将注释添加到地图并立即选择它。
尽管如此,运行 iOS 5.1.1 的未越狱 iPhone 4S 上的这段代码并未选择注释(未显示标注),但这在 iOS 6 模拟器中运行良好。
为了解决这个问题,我做了以下操作,这基本上将引脚的选择延迟了 0.2 秒,这是我不喜欢的:
self.departureAnnotation = [[UserAnnotation alloc] init];
self.departureAnnotation.coordinate = self.map.centerCoordinate;
[self.map addAnnotation:self.departureAnnotation];
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(selectPin:) userInfo:self.departureAnnotation repeats:NO];
- (void)selectPin:(NSTimer *)timer
{
[self.map selectAnnotation:timer.userInfo animated:YES];
}
为什么会这样?
PS:此外,按照相同的模式,如果我检查[self.map viewForAnnotation: self.departureAnnotation]
而不是选择引脚,则视图为零。 延迟 0.2 秒后,就可以了。
MKMapView
可能需要运行循环循环才能完成,然后才能为您提供注释视图/选择注释。 如果是这样,请不要使用计时器,将您的selectAnnotation:animated:
分派到下一个运行循环周期...
dispatch_async( dispatch_get_main_queue(), ^{
[self.map selectAnnotation:self.departureAnnotation animated:YES];
} );
...可能会有所帮助。
文档还指出您需要立即添加注释,因为MKMapView
决定哪个注释在屏幕上/屏幕外,因此它会返回或不返回视图。
只是我的 0.02 美元...
这可能是因为CATransaction
( docs )。 CATransaction
“收集”您在开始和提交状态之间对 CALayer 所做的所有更改。 有时,可能由于不正确的编程技术,两种方法发生“冲突”,通常是在它们异步时。 对我来说,它是使用按钮发生的,比如当你激活一个按钮并想同时停用其他按钮时。 我想其他解决方法可能是打电话[self.map setNeedsDisplay];
在[self.map addAnnotation:self.departureAnnotation];
或在相同的方法后使用这段代码:
id __weak weakSelf = (id)self;
[CATransaction setCompletionBlock:^{[weakSelf.map selectAnnotation:weakSelf.departureAnnotation animated:YES];}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.