2017-07-19 87 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你对我的解决方案有什么经验? –

相关问题