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 pos开发者_开发技巧sible 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.
You can send a delayed message with performSelector:withObject:afterDelay:
or one of its related methods.
精彩评论