本文介绍了iOS MKMapView-动画地图图钉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

向图钉添加动画,以添加图钉以平滑地映射.在地图上添加图钉时,它不平滑或没有动画效果.有没有简单的解决方案来实现这一目标?

Add animation to pins for adding pins to map smoothly.While adding pins to map, it is not smooth or it has no animation effect.Is there any simple solution to achieve this?

推荐答案

我用简单的代码尝试了

我创建了一个简单的代码来快速解决此问题.

I have created a simple code to fix this issue in swift 4.

这是我的代码,

像这样覆盖您的viewForAnnotaion方法

Override your viewForAnnotaion method something like this

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类,请参考此链接

For AnimationUtility class refer this link

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

此代码添加了图钉以使用平滑的动画进行映射.

This code adds pins to map with smooth animation.

这篇关于iOS MKMapView-动画地图图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 15:22