簡體   English   中英

iOS MKMapView-動畫地圖圖釘

[英]iOS MKMapView - Animate map Pins

使用Swift 4的iOS中的MKAnnotationView動畫

向圖釘添加動畫,以添加圖釘以平滑映射。 在地圖上添加圖釘時,它不平滑或沒有動畫效果。 有沒有簡單的解決方案來實現這一目標?

我用簡單的代碼嘗試了

在此處輸入圖片說明

我創建了一個簡單的代碼來快速解決此問題。

這是我的代碼,

像這樣重寫您的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

這段代碼添加了圖釘以平滑的動畫進行映射。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM