MKMapView setRegion how to stop animation

2019-07-26 07:26发布

问题:

can u help me? I need to stop MkMapView method setRegion:region animated: true. I need to set new region animated with option to stop it. Thanks.

回答1:

If you create another animation but for a different region than the existing one, this will invalidate the existing one. Note: If I try to create a new animation but for same the visibleMapRect the iOS will just ignore it.

func stopZoom() {
  //the trick: creating a region very similar to the existing one
  var mapRegion:MKCoordinateRegion = MKCoordinateRegionForMapRect(self.mapView.visibleMapRect)
  mapRegion.span.latitudeDelta = mapRegion.span.latitudeDelta + 0.000001

  UIView.animateWithDuration(0.1, delay: 0.0, options: [], animations: {
    let mapRegion:MKCoordinateRegion = mapRegion 
    self.mapView.setRegion(mapRegion, animated: true) //this will invalidate the other animations
  }) { (completed: Bool) -> Void in
}

}

for reference the startZoom method:

func startZoom() {
  UIView.animateWithDuration(10, delay: 0.0, options: [UIViewAnimationOptions.CurveLinear, UIViewAnimationOptions.AllowUserInteraction, UIViewAnimationOptions.BeginFromCurrentState], animations: {
  let mapRegion:MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(self.coordinate, 500, 500)
  self.mapView.setRegion(mapRegion, animated: true)
  }) { (completed: Bool) -> Void in
}

}

Spent quite a lot of time to figure this out, I hope it will be helpful to you.



回答2:

One thing you might try is creating a CADisplayLink that is tied to the screen refresh rate, then in its callback, checking the current state of the map. There you can call another setRegion:animated: to the current position in order to stop the previous animation.



回答3:

The most easiest way is to nil the delegate of MKMapView, so that no delegate method will be called after it.

self.mapView.delegate = nil;