自定义图钉动画 - MKMapView
Posted
技术标签:
【中文标题】自定义图钉动画 - MKMapView【英文标题】:Custom pin animation - MKMapView 【发布时间】:2010-04-16 19:01:38 【问题描述】:我在应用程序中使用了 pin 图像而不是标准 pin,现在我想为自定义 pin 提供动画(与标准 pin 一样的掉落效果)。如何为自定义 pin 图像提供下降动画效果???
【问题讨论】:
This should help 【参考方案1】:实现以下委托方法。
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
MKAnnotationView *aV;
for (aV in views)
CGRect endFrame = aV.frame;
aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 230.0, aV.frame.size.width, aV.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.45];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[aV setFrame:endFrame];
[UIView commitAnimations];
【讨论】:
【参考方案2】:如果你不一次丢掉所有的大头针,而是稍微延迟地丢掉每个大头针,这样看起来就会有大雨般的大头针效果,这也会让人感觉凉爽很多。类似于 Apple 在本地所做的。使用这个:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
MKAnnotationView *aV;
float delay = 0.00;
for (aV in views)
CGRect endFrame = aV.frame;
aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 430.0, aV.frame.size.width, aV.frame.size.height);
delay = delay + 0.01;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:delay];
[UIView setAnimationDuration:0.45];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[aV setFrame:endFrame];
[UIView commitAnimations];
【讨论】:
【参考方案3】:这对我来说效果很好。不记得我在哪里找到它了
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
MKAnnotationView *aV;
for (aV in views)
// Don't pin drop if annotation is user location
if ([aV.annotation isKindOfClass:[MKUserLocation class]])
continue;
// Check if current annotation is inside visible map rect, else go to next one
MKMapPoint point = MKMapPointForCoordinate(aV.annotation.coordinate);
if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point))
continue;
CGRect endFrame = aV.frame;
// Move annotation out of view
aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - self.view.frame.size.height, aV.frame.size.width, aV.frame.size.height);
// Animate drop
[UIView animateWithDuration:0.3 delay:0.03*[views indexOfObject:aV] options:UIViewAnimationCurveLinear animations:^
aV.frame = endFrame;
// Animate squash
completion:^(BOOL finished)
if (finished)
[UIView animateWithDuration:0.05 animations:^
aV.transform = CGAffineTransformMakeScale(1.0, 0.8);
completion:^(BOOL finished)
if (finished)
[UIView animateWithDuration:0.5 animations:^
aV.transform = CGAffineTransformIdentity;
];
];
];
【讨论】:
【参考方案4】:请注意,当MKMapView.userTrackingMode
等于MKUserTrackingModeFollowWithHeading
时,为-mapView:didAddAnnotationViews:
中的注释视图设置动画会导致奇怪的效果。我只希望 Apple 为 MKAnnotationView
类提供拖放动画。
【讨论】:
【参考方案5】:Swift 3拖放动画
// animate annotation views drop
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView])
for annView in views
// animate any annotation views except the user pin
if !(annView.annotation?.isKind(of: MKUserLocation.self))!
let endFrame = annView.frame
annView.frame = endFrame.offsetBy(dx: 0, dy: -500)
UIView.animate(withDuration: 0.5, animations:
annView.frame = endFrame
)
【讨论】:
以上是关于自定义图钉动画 - MKMapView的主要内容,如果未能解决你的问题,请参考以下文章