Reputation: 319
I want to animate an coordinate change of an MKPointAnnotation on a map view like this:
let destinationLocation = CLLocationCoordinate2D(latitude: 51.873983, longitude: 12.627966)
UIView.animate(withDuration: 20, animations: {
self.myAnnotation.coordinate = destinationLocation
}, completion: nil)
I added the pin on the map like this:
let startPosition = CLLocationCoordinate2D(latitude: 53.014542, longitude: 13.996593)
myAnnotation.coordinate = startPosition
mapView.addAnnotation(myAnnotation)
myAnnotation is a global variable declared like this in my ViewController:
private let myAnnotation = MKPointAnnotation()
But when I zoom the map during the animation (method regionDidChangeAnimated from the MKMapViewDelegate is called) the position of the pin is moving to an wrong location on the map and not to the correct map coordinate during zooming.
I upload an Screencast here which describes the problem very well.
I don't know another possibility to provide a short video. The size is only 8 MB. That is the best way to see the issue.
Upvotes: 2
Views: 1788
Reputation: 1252
Whenever the user interacts with the map (zoom, scroll) you could/should stop the animation
if let annotationView = self.mapView.view(for: self.myAnnotation) {
annotationView.layer.removeAllAnimations()
}
Furthermore you can implement the completion handler of your animation to act correctly on a canceled/successful animation:
UIView.animate(withDuration: 20, animations: {
self.myAnnotation.coordinate = destinationLocation
}, completion: { success in
if success {
// handle a successfully ended animation
} else {
// handle a canceled animation, i.e move to destination immediately
self.myAnnotation.coordinate = destinationLocation
}
})
Upvotes: 3