cocoa-touchios-4.2mkannotationviewmkpinannotationviewmapkit

How do I animate MKAnnotationView drop?


I have a custom MKAnnotationView where I set my image myself in viewForAnnotation. How do I animate it's drop like I can with MKPinAnnotationView?

My code is

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"blah.png"];
    annotationView.annotation = annotation;

    return annotationView;
}

Solution

  • Implement the didAddAnnotationViews delegate method and do the animation yourself:

    - (void)mapView:(MKMapView *)mapView 
              didAddAnnotationViews:(NSArray *)annotationViews
    {
        for (MKAnnotationView *annView in annotationViews)
        {
            CGRect endFrame = annView.frame;
            annView.frame = CGRectOffset(endFrame, 0, -500);
            [UIView animateWithDuration:0.5 
                    animations:^{ annView.frame = endFrame; }];
        }
    }