简体   繁体   中英

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. 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. I basically created a member variable in the custom class that I set in the mapView annotation delegate method as seen below.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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