MKMapView RegionDidChangeAnimated 由用户操作或程序调用
我有一个 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当点击地图视图边缘附近的注释并移动地图以适合标注时,事件顺序为:
regionWillChangeAnimated
被调用didSelectAnnotationView
被调用regionDidChangeAnimated
被调用使用两个
BOOL
ivar 标志,您可以监视此序列并防止在regionDidChangeAnimated
中重新加载注释。例如:
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
is calleddidSelectAnnotationView
is calledregionDidChangeAnimated
is calledUsing two
BOOL
ivar flags, you can watch for this sequence and prevent the re-loading of annotations inregionDidChangeAnimated
.For example:
您删除所有注释并在
regionDidChangeAnimated
方法中添加新注释吗?我认为更强大的解决方案是使用字典和一些唯一标识符作为键(不会改变,数据库 ID 等)来跟踪您添加到地图中的所有注释。然后,在您的regionDidChangeAnimated
方法中,您只添加实际的新注释,也可能删除区域之外的注释。You remove all annotations and add new ones in your
regionDidChangeAnimated
method? I think a more robust solution is to keep track of all annotations you have added to the map using a dictionary and some unique identifier as key (that will not change, database id etc.) . Then in yourregionDidChangeAnimated
method you only add actually new ones and maybe also remove annotations outside the region.您可能会发现将加载新注释与 UIGestureRecognizer 联系起来是一种更好的体验,因此只有当您知道用户已手动滚动地图时,您才加载新注释。这还可以防止设备旋转时重新加载。请参阅 Jano 的回答确定 MKMapView 是否被拖动/移动。
You may find that it's a better experience to tie loading your new annotations to a UIGestureRecognizer, so you are only loading the new ones when you know for a fact that the user has scrolled the map manually. This can also prevent reloading when the device is rotated. See Jano's answer at determine if MKMapView was dragged/moved.