Delegate Method Call Delay - mapView: regionDidChangeAnimated:

Whenever a user scrolls a map or scales input / output, this method is called instantly. I want to defer calling this method, say, 2 seconds. Is it possible to do this?

+3
source share
2 answers

You can implement this method as follows:

-(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 these arguments (either mapView, or animated), you can do it much easier just by passing the one you need.

MKMapViewDelegate, , - swizzling, .

+4

performSelector:withObject:afterDelay: .

0

Source: https://habr.com/ru/post/1741613/


All Articles