Objective-C for Dummies: How to change the region without invoking mapView:regionDidChangeAnimated?
so I have my self a conundrum. I'm changing the region of an MKMapView in code, which works, however it invokes the mapView:regionDidChangeAnimated
which cancels my check if the user is the one actually moving the map. So, I'm having a difficult time trying to figure out how to manually set region in code while also checking if the user changes it (it's also 2:30 AM and my brain isn't really cooperating...). Anyway, here's my code:
- (void)displayMyLocation:(CLLocation *)location {
if (!userChangedRegion) {
MKC开发者_如何学CoordinateSpan span;
MKCoordinateRegion region;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.center = location.coordinate;
region.span = span;
[map setRegion:region];
}
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
userChangedRegion = YES;
}
Thanks in advance!
If you're just trying to skip setting userChangedRegion
when you change the region, you could use a second variable programChangedRegion
to track whether you are currently setting the region.
For example:
programChangedRegion = YES;
[map setRegion:region];
}
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
if (programChangedRegion == YES) {
programChangedRegion = NO;
} else {
userChangedRegion = YES;
}
}
精彩评论