比较两个数组值CGRectIntersectsRect
问题描述:
地图上有注释(可视标签)。当用户在地图中缩放(放大或缩小)时,会调用名为'regionDidChangeAnimated'的方法。每次完成时,我尝试使用快速枚举来查看哪些注记对象当前与地图上的哪些注记对象相交。这是因为我使用的是YandexMapKit(不是苹果标准的MapKit),并且没有太多关于如何以其他方式完成这些任务的文档或示例。下面是唯一的代码,我能想出这么远,但它最终崩溃的应用程序:比较两个数组值CGRectIntersectsRect
NSArray *allAnnotations = [_mapView annotations];
// Check for intersecting annotation bounds.
for (RBMapAnnotation *annotation1 in allAnnotations)
{
YMKPinAnnotationView *pinView1 = (YMKPinAnnotationView *)annotation1; // Still returns as 'RBMapAnnotation'.
for (RBMapAnnotation *annotation2 in allAnnotations)
{
YMKPinAnnotationView *pinView2 = (YMKPinAnnotationView *)annotation2;
if (CGRectIntersectsRect([pinView1 convertRect:pinView1.bounds toView:nil], [pinView2 convertRect:pinView2.bounds toView:nil]))
{
NSLog(@"Interesction between %@ and %@", annotation1.offer.name, annotation2.offer.name);
//[annotationsToUpdate addObject:annotation];
}
}
}
请帮我出这一点。我试图获得的是,如果任何注释在视觉上都在地图上相交,那么我将它们都删除并用1个注释替换它们。
答
忽略算法效率低下(您可能要返工此,所以它不是O(N^2))...
你提到的代码崩溃,但没有提供任何诊断/回溯以协助诊断碰撞。
您正在循环注释,将它们投射到YMKPinAnnotationView
,并且他们将它们视为这样。这很可能是你的崩溃的根源。看来注释是符合YMKAnnotation
的实例,但实际上并不是视图。您应该发送地图视图-viewForAnnotation:
传递注释以获取关联的视图。
+0
谢谢。碰撞期间的诊断没有任何有用的信息。但后来我发现它必须与铸造到YMKPinAnnotationView。 – Krekin
@Alex for适用于符合NSFastEnumeration的任何对象。 NSEnumerator符合NSFastEnumeration。 –
我将其更改为标准'[allAnnotations reverseObjectEnumerator]'为'allAnnotations'。但它仍然崩溃。并不是说它应该有所作为。 – Krekin
提供有关崩溃的详细信息。哪条线完全崩溃,错误信息是什么? – rmaddy