我尝试为我的自定义 MkAnnotation 设置动画,它在 iOS 5 上运行良好,但在 iOS 6 上运行良好。这是我的 didAddAnnotationViews 方法:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)annotationViews
{
NSTimeInterval delayInterval = 0;
for (MKAnnotationView *annView in annotationViews)
{
// Don't pin drop if annotation is user location
if ([annView.annotation isKindOfClass:[MKUserLocation class]]) {
continue;
}
// Check if current annotation is inside visible map rect, else go to next one
MKMapPoint point = MKMapPointForCoordinate(annView.annotation.coordinate);
if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point)) {
continue;
}
CGRect endFrame = annView.frame;
// Move annotation out of view
annView.frame = CGRectMake(annView.frame.origin.x, annView.frame.origin.y - self.view.frame.size.height, annView.frame.size.width, annView.frame.size.height);
[UIView animateWithDuration:1
delay:delayInterval
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
annView.frame = endFrame;
}
completion:^(BOOL finished){
if (isModal)
[self.mapView selectAnnotation:[[self.mapView annotations] objectAtIndex:0] animated:YES];
}];
delayInterval += 0.0625;
}
}
我用我在互联网上找到的部分代码制作了这个方法。在 iOS 5 上,动画是完美的,但在 iOS 6 上,pin 只是出现,没有任何动画。设置 mapView 委托是我在 viewDidLoad 上做的第一件事,我还尝试从 viewDidAppear 方法生成注释,但没有成功。
任何的想法 ?
谢谢。
编辑:找到解决方案,我使用 perform:withObject:afterDelay: 方法,它似乎工作。
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
[self performSelector:@selector(addAnnotation) withObject:nil afterDelay:0.0];
}
- (void)addAnnotation
{
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:@"test" andCoordinate:CLLocationCoordinate2DMake(49.6, 6.2)];
[mapView addAnnotation:annotation];
}