9

你知道 MKPinAnnotationView 有一个方法“animatesDrop”,可以用阴影从顶部到地图上的点对 pin 注释进行动画处理?好的,是否可以使用自定义图像执行此操作?

4

4 回答 4

22

您始终可以在 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];
    }
}

斯威夫特版本

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()
    }
}
于 2010-01-26T12:35:47.170 回答
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()
    }
}
于 2015-08-08T10:34:09.523 回答
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];
 }
}
于 2013-01-01T10:46:56.150 回答
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)
    }
}
于 2020-09-28T03:43:10.213 回答