简体   繁体   中英

Delay the call to the delegate method - mapView:regionDidChangeAnimated:

Whenever the user scrolls map or zooms in/out, this method gets called instantaneously. I want to delay the call to this method by say 2 secs. Is it possible to do that?

You could implement that method like this:

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSNumber *animatedNumber = [NSNumber numberWithBool:animated];
    NSArray *args = [[NSArray alloc] initWithObjects:mapView,
                                                     animatedNumber,nil];

    [self performSelector:@selector(delayedMapViewRegionDidChangeAnimated:)
          withObject:args
          afterDelay:2.0f];

    [args release];
}

Then, somewhere in the same class:

-(void)delayedMapViewRegionDidChangeAnimated:(NSArray *)args
{
  MKMapView *mapView = [args objectAtIndex:0];
  BOOL animated = [[args objectAtIndex:1] boolValue];

  // do what you would have done in mapView:regionDidChangeAnimated: here
}

Of course, if you don't need one of those arguments (either mapView or animated ), you could make this considerably simpler by only passing the one you did need.

If you can't just edit the code for your MKMapViewDelegate , perhaps you could do something similar with method swizzling, although then you're getting really hacky.

您可以使用performSelector:withObject:afterDelay:或其相关方法之一发送延迟消息。

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