MKMapView regionDidChangeAnimated 由用户操作或程序调用 [英] MKMapView regionDidChangeAnimated called by user action or program
问题描述
我有一个带有一些注释的 MKMapView.现在,每当区域发生变化时,我都会加载新的注释.这工作正常,但是如果某个注释靠近地图视图的边界并且您点击它,注释信息窗口会弹出并且 mkmapview 区域移动一点(以便它可以很好地显示窗口),但问题是还调用 regionDidChangeAnimated 并重新加载我的所有注释,当然还隐藏了信息窗口.
我知道您可以在重新加载注释时再次点击该注释,但对于用户而言,它似乎已损坏,并且您还可以在不需要时重新加载注释.
有什么方法可以检查 regionDidChangeAnimated 是否由于用户操作或以编程方式被调用?
谢谢.
I have a MKMapView with some annotations in it. Now, every time when the region changes I load new annotations. That works fine, but if some annotation is near border of the map view and you tap on it, the annotation info window pops out and the mkmapview region moves a little (so that it can show the window nicely), but the problem is that also the regionDidChangeAnimated is called and it reloads all my annotations and of course hides the info window.
I know you can just tap the annotation once again when it's reloaded but for user it seems broken and also you reload annotations when you don't need to.
Is there any way to check whether the regionDidChangeAnimated was called because of a user action or programatically?
Thanks.
推荐答案
当地图视图边缘附近的注释被点击并移动地图以适应标注时,事件序列为:
When an annotation near the map view edge is tapped and it moves the map to fit the callout, the sequence of events is:
regionWillChangeAnimated
被调用didSelectAnnotationView
被调用regionDidChangeAnimated
被调用
regionWillChangeAnimated
is calleddidSelectAnnotationView
is calledregionDidChangeAnimated
is called
使用两个 BOOL
ivar 标志,您可以监视此序列并防止重新加载 regionDidChangeAnimated
中的注释.
Using two BOOL
ivar flags, you can watch for this sequence and prevent the re-loading of annotations in regionDidChangeAnimated
.
例如:
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
regionWillChangeAnimatedCalled = YES;
regionChangedBecauseAnnotationSelected = NO;
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
regionChangedBecauseAnnotationSelected = regionWillChangeAnimatedCalled;
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
if (!regionChangedBecauseAnnotationSelected) //note "!" in front
{
//reload (add/remove) annotations here...
}
//reset flags...
regionWillChangeAnimatedCalled = NO;
regionChangedBecauseAnnotationSelected = NO;
}
这篇关于MKMapView regionDidChangeAnimated 由用户操作或程序调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!