0

使用 Swift 4 的 iOS 中的 MKAnnotationView 动画

将动画添加到引脚以添加引脚以平滑映射。为地图添加图钉时,不流畅或没有动画效果。有没有简单的解决方案来实现这一目标?

4

1 回答 1

1

我用简单的代码试过了

在此处输入图像描述

我在 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

此代码将引脚添加到具有平滑动画的地图。

于 2018-03-28T14:48:12.513 回答