iOS MKMapView – 动画地图别针

使用Swift 4在iOS中使用MKAnnotationView动画

添加动画到引脚以添加引脚以顺利地映射。 添加引脚进行映射时,它不是平滑的,也没有动画效果。 有没有简单的解决方案来实现这一目标?

我用简单的代码试了一下

在此处输入图像描述

我已经创建了一个简单的代码来解决swift 4中的这个问题。

这是我的代码,

覆盖你的viewForAnnotaion方法

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin") if annotationView == nil{ annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Pin") }else{ annotationView?.annotation = annotation } annotationView?.canShowCallout = false guard !annotation.isKind(of: MKUserLocation.self) else { //for the custom image on current location annotationView?.image = #imageLiteral(resourceName: "currentLocationPin") return annotationView } annotationView.image = UIImage(named: "pinGreen") let scaleTransform = CGAffineTransform(scaleX: 0.0, y: 0.0) // Scale UIView.animate(withDuration: 0.2, animations: { annotationView?.transform = scaleTransform annotationView?.layoutIfNeeded() }) { (isCompleted) in // Nested block of animation UIView.animate(withDuration: 0.3, animations: { annotationView?.alpha = 1.0 annotationView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) AnimationUtility.viewSlideInFromBottom(toTop: annotationView!) annotationView?.layoutIfNeeded() }) } return annotationView } 

对于AnimationUtility类,请参阅此链接

https://stackoverflow.com/a/49398148/8334818

此代码添加引脚以使用平滑动画进行映射。