【发布时间】:2010-01-26 12:07:58
【问题描述】:
你知道 MKPinAnnotationView 有一个方法“animatesDrop”,可以用阴影在地图上从顶部到点对 pin 注释进行动画处理?好的,是否可以使用自定义图像执行此操作??
【问题讨论】:
标签: iphone view mkannotation
你知道 MKPinAnnotationView 有一个方法“animatesDrop”,可以用阴影在地图上从顶部到点对 pin 注释进行动画处理?好的,是否可以使用自定义图像执行此操作??
【问题讨论】:
标签: iphone view mkannotation
您始终可以在 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()
}
}
【讨论】:
与@gcamp 相同的答案仅适用于感兴趣的人在 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()
}
}
【讨论】:
感谢@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];
}
}
【讨论】:
为 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)
}
}
【讨论】: