2017-07-19 78 views
1

當我嘗試訪問MKAnnotationView的動畫拖放屬性時,它不存在。我知道這個屬性存在MKPinAnnotationView,但我使用自定義圖像,因此必須使用MKAnnotationView。任何方式來動畫的MKAnnotationView的下降?代碼:如何使用自定義圖像對MKAnnotationView進行動畫製作

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { return nil } 

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinIdentifier) 
    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier) 
     annotationView?.canShowCallout = true 

     let pinImage = UIImage(named:"mappin")! 
     annotationView?.image = pinImage 
     //annotationView?.animatesDrop = true 

    } else { 
     annotationView?.annotation = annotation 
    } 
    return annotationView 
} 
+0

我的回答對你有幫助嗎? –

回答

0

1)創建一個xib文件和相關文件.swift握住你的AnimatedAnnotation代碼。

pics

2)AnimatedAnnotation.xib你可以把你的UI創造力任務和創建註釋。

enter image description here

3)在你AnimatedAnnotation.swift文件,創建自己的AnimatedAnnotation類。

class AnimatedAnnotation: MKAnnotationView { 


} 

4)接着,寫,將配置該「不可見」容器視圖的實例與animationDuration的配置方法,以及annotationImage(這將是您的自定義圖像)。

class AnimatedAnnotation: UIView { 

    var animationDuration: Double! 
    var annotationImage: UIImage! 

    func configureWith(animationDuration: Double, annotationImage: UIImage) { 
     self.backgroundColor = .clear 
     self.annotationImage = annotationImage 
     self.animationDuration = animationDuration 
    } 
} 

5)然後聲明UIImageView類型的屬性animatedView和配置塊內實例化。

class AnimatedAnnotation: UIView { 

    var animatedView: UIImageView! 
    var animationDuration: Double! 
    var annotationImage: UIImage! 

    func configureWith(animationDuration: Double, annotationImage: UIImage) { 
     self.backgroundColor = .clear 
     self.annotationImage = annotationImage 
     self.animationDuration = animationDuration 
     instantiateAnimatedView() 
    } 

    func instantiateAnimatedView() { 
     let startingPosition = CGRect(x: 0, y: 0, width: 20, height: 20) // This is whatever starting position you want 
     let imageView = UIImageView(frame: startingPosition) 
     imageView.image = UIImage(name: "YourAnnotationImage") 
     imageView.alpha = 0 
     addSubview(imageView!) 
    } 
} 

6)寫的方法,使動畫視圖,並顯示在上面動畫的它的「隱形」上海華。

class AnimatedAnnotation: UIView { 

    var animatedView: UIImageView! 
    var animationDuration: Double! 
    var annotationImage: UIImage! 

    func configureWith(animationDuration: Double, annotationImage: UIImage) { 
     self.backgroundColor = .clear 
     self.annotationImage = annotationImage 
     self.animationDuration = animationDuration 
     instantiateAnimatedView() 
     dropAnnotation() // Call whenever you want the drop to happen 
    } 

    func instantiateAnimatedView() { 
     let startingPosition = CGRect(x: 0, y: 0, width: 20, height: 20) // This is whatever starting position you want 
     let imageView = UIImageView(frame: startingPosition) 
     imageView.image = annotationImage 
     imageView.alpha = 0 
     addSubview(imageView!) 
    } 

    func dropAnnotation() { 
     let dropHeight:CGFloat = self.bounds.size.height - animatedView.frame.size.height 
     let endPosition = CGPoint(x: animatedView.center.x, y: animatedView.center.y + dropHeight) 
     UIView.animate(withDuration: animationDuration, animations: { 
      self.animatedView.alpha = 1 
      self.animatedView.center = endPosition 
     }) 
    } 
} 

7)的使用viewFor annotation: MKAnnotation) -> MKAnnotationView?方法,實例化一個AnimatedAnnotation並返回的方法。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    let bundle = Bundle.main // or your framework bundle 
    let xib = UINib(nibName: "AnimatedAnnotation", bundle: bundle) 
    let view = xib.instantiate(withOwner: self, options: nil)[0] as! AnimatedAnnotation 
    view.configureWith(animationDuration: 0.25, annotationImage: UIImage(named: "Your-Annotation-Image") 
    return view 
} 

我並沒有真正在Xcode嘗試這樣做,我只是寫了這個在SO編輯器,但這應該工作。讓我知道你的進步。

+0

謝謝。我會試試這個,讓你知道 – Shekar

+0

@Shekar你對我的解決方案有什麼經驗? –

相關問題