简体   繁体   English

在MKMapView Swift 3 iOS上停止所有动画

[英]Stopping all animations in place on MKMapView Swift 3 iOS

I have been stuck on this issue for the past day. 在过去的一天中,我一直在这个问题上停留。 I have created a custom MKAnnotation subclass to display various custom pins on an MKMapView. 我创建了一个自定义MKAnnotation子类,以在MKMapView上显示各种自定义引脚。 I recursively call a function that keeps animating these pins around the map. 我递归地调用一个函数,使这些图钉始终围绕地图移动。 My goal is to stop all of these animations in place when the user taps on a button. 我的目标是当用户点击按钮时将所有这些动画停止在适当的位置。 I have tried 我努力了

self.view.layer.removeAllAnimations()

and

self.map.layer.removeAllAnimations()

and other hacky solutions, but none seem to work. 和其他骇人听闻的解决方案,但似乎无济于事。

Below is the code that creates the animation/pin movement 以下是创建动画/图钉运动的代码

func animate(duration:Double, newLocation:CLLocationCoordinate2D){
    UIView.animate(withDuration: duration, animations: {
        self.coordinate = newLocation
    }) { (done) in
        self.finished_segment()
    }
} 

Any suggestions are much appreciated. 任何建议,不胜感激。

For anyone stuck on this issue. 对于陷在这个问题上的任何人。 The problem was that I had to remove the animation from the MKAnnotationView associated with the annotation. 问题是我必须从与注释关联的MKAnnotationView中删除动画。 I basically created a member variable in the custom class that I set in the mapView annotation delegate method as seen below. 我基本上在自定义类中创建了一个成员变量,该自变量类是在mapView注释委托方法中设置的,如下所示。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationView") ?? MKAnnotationView()
    if let a = annotation as? Alien{
        annotationView.image = a.image
        a.annotationView = annotationView
    }

    return annotationView
}

And to remove the animation from the map. 并从地图上删除动画。 Simply call 只需致电

self.annotationView.layer.removeAllAnimations()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM