是否可以在MKAnnotationView而不是MKPinAnnotationView中调用animatesDrop?

时间:2010-01-26 12:07:59

标签: iphone view mkannotation

你知道MKPinAnnotationView有一个方法“animatesDrop”来动画一个引脚注释从地点到顶部用阴影指向它吗?好的,是否可以使用自定义图像执行此操作?

4 个答案:

答案 0 :(得分:21)

您可以随时在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];
    }
}

Swift版本

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()
    }
}

答案 1 :(得分:3)

对于那些感兴趣的人来说,仅在Swift中与@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()
    }
}

答案 2 :(得分: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 :(得分:0)

已针对ios 14更新

func mapView(_ mapView: MKMapView, didAdd 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.animate(withDuration: 1.5, animations: {
            view.frame = endFrame
        }, completion: nil)
    }
}