2010-01-26 79 views

回答

20

您可以隨時在MKMapViewDelegate方法中執行自定義動畫。

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views 

也許類似的東西(你不會得到花哨的影子動畫,如果你想它,你需要自己做):

- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    CGRect visibleRect = [mapView annotationVisibleRect]; 
    for (MKAnnotationView *view in views) { 
     CGRect endFrame = view.frame; 

     CGRect startFrame = endFrame; startFrame.origin.y = visibleRect.origin.y - startFrame.size.height; 
     view.frame = startFrame; 

     [UIView beginAnimations:@"drop" context:NULL]; 
     [UIView setAnimationDuration:1]; 

     view.frame = endFrame; 

     [UIView commitAnimations]; 
    } 
} 
+0

@gcamp:我也嘗試以這種方式,但無法正常工作(圖像不改變): MKPinAnnotationView * annView = [MapView的dequeueReusableAnnotationViewWithIdentifier:ident]上; annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ident] autorelease]; annView.image = [UIImage imageNamed:@「annotImg.png」]; annView.animatesDrop = TRUE; – Mat 2010-01-26 12:45:37

+0

更改了我的答案! – gcamp 2010-01-26 13:04:57

+0

非常感謝!這對我來說非常有用! – Mat 2010-01-26 19:59:58

1

感謝@gcamp的回答,它的工作原理不錯,但我稍作修改是準確的,其視圖將在MapView的被丟棄,請檢查下面的代碼:只在斯威夫特

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views 
{ 
CGRect visibleRect = [mapView annotationVisibleRect]; 
for (MKAnnotationView *view in views) 
{ 
    CGRect endFrame = view.frame; 
    endFrame.origin.y -= 15.0f; 
    endFrame.origin.x += 8.0f; 
    CGRect startFrame = endFrame; 
    startFrame.origin.y = visibleRect.origin.y - startFrame.size.height; 
    view.frame = startFrame; 

    [UIView beginAnimations:@"drop" context:NULL]; 
    [UIView setAnimationDuration:0.2]; 

    view.frame = endFrame; 

    [UIView commitAnimations]; 
} 
} 
3

相同的答案@gcamp對於那些有興趣

func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) { 
    let visibleRect = mapView.annotationVisibleRect 

    for view:MKAnnotationView in views{ 
     let endFrame:CGRect = view.frame 
     var startFrame:CGRect = endFrame 
     startFrame.origin.y = visibleRect.origin.y - startFrame.size.height 
     view.frame = startFrame; 

     UIView.beginAnimations("drop", context: nil) 
     UIView.setAnimationDuration(1) 

     view.frame = endFrame; 

     UIView.commitAnimations() 
    } 
} 
相關問題