MKMapView RegionDidChangeAnimated 由用户操作或程序调用

发布于 2024-12-14 09:41:13 字数 312 浏览 0 评论 0原文


我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少心凉 2024-12-21 09:41:13

当点击地图视图边缘附近的注释并移动地图以适合标注时,事件顺序为:

  1. regionWillChangeAnimated 被调用
  2. didSelectAnnotationView 被调用
  3. regionDidChangeAnimated 被调用

使用两个 BOOL ivar 标志,您可以监视此序列并防止在 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;
}

When an annotation near the map view edge is tapped and it moves the map to fit the callout, the sequence of events is:

  1. regionWillChangeAnimated is called
  2. didSelectAnnotationView is called
  3. regionDidChangeAnimated is called

Using two BOOL ivar flags, you can watch for this sequence and prevent the re-loading of annotations in regionDidChangeAnimated.

For example:

-(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;
}
行雁书 2024-12-21 09:41:13

您删除所有注释并在 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 your regionDidChangeAnimated method you only add actually new ones and maybe also remove annotations outside the region.

转身以后 2024-12-21 09:41:13

您可能会发现将加载新注释与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文