为什么这不起作用?
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
if (TRACE_LOG) NSLog(@"%s", __FUNCTION__);
[mapView selectAnnotation:[views lastObject] animated:YES];
return;
}
谢谢,Z@K!
为什么这不起作用?
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
if (TRACE_LOG) NSLog(@"%s", __FUNCTION__);
[mapView selectAnnotation:[views lastObject] animated:YES];
return;
}
谢谢,Z@K!
因为您必须选择注释对象,而不是与其对应的视图。
我100%不确定,但我认为以下应该可行:
MKAnnotationView* annotationView = (MKAnnotationView*)[views lastObject];
[mapView selectAnnotation:annotationView.annotation animated:YES];
如果您将注释存储在某处,那么直接从该存储中获取所需的注释对象可能会更好。请注意,只有当您尝试选择当前在屏幕上可见的注释时,所有这些方法才会生效。
如果您只有一个注释,这是最简单的方法:
[mapView selectAnnotation:[mapView.annotations objectAtIndex:0] animated:true];
注意:如果您启用了 pin drop 动画,这将禁用它。基本上,它会在添加到地图视图后立即选择注释,从而切断动画。
如果您想在选择它之前等待大头针放置动画完成,这里有一种半hacky的方法来实现它。首先,确保您的视图控制器设置为 MKMapViewDelegate 然后:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.7 target:self selector:@selector(pinDropped) userInfo:nil repeats:NO];
}
- (void) pinDropped {
[mapView selectAnnotation:[mapView.annotations objectAtIndex:0] animated:true];
}