12

我之前创建了一个自定义标注气泡作为 MKAnnotationView 的子视图,因为内置标注非常有限。这需要我在选择 MKAnnotationView 以考虑标注气泡的大小时更改为 centerOffset。这一切都在 iOS 4 出现之前完美运行。现在,在 iOS 4 中,它完全忽略了我对 centerOffset 属性的更新,因此图钉和气泡似乎向下和向右跳(标注气泡的左上角现在位于图钉点应该在的位置) .

有谁知道为什么这在 iOS 4 中发生了变化?我可以做些什么来让 MKMapView 识别新的 centerOffset?这是苹果引入的错误吗?

谢谢您的帮助!

4

3 回答 3

6

确保您使用的是MKAnnotationView而不是MKPinAnnotationView!您不能设置 MKPinAnnotationView 对象的centerOffset(当然,除非您是子类)。

于 2014-01-22T15:45:06.780 回答
4

我有同样的问题 - centerOffset 似乎只是第一次考虑。它在内部进行了更改,但视图没有移动 - 所以您需要自己移动视图。

您可以通过使用所需的偏移量调整其中心来移动视图 - 选定的视图在左上角与未选定的视图保持对齐,因此您需要重新对齐它们的中心。这是我的情况:

选中 -> 未选中:

self.center = CGPointMake(self.center.x + 56.0, self.center.y + 130.0);
self.centerOffset = CGPointMake(5.0, -14.0);

未选中 -> 选中:

self.center = CGPointMake(self.center.x - 56.0, self.center.y - 130.0);
self.centerOffset = CGPointMake(64.0, -81.0);

其中 130 是视图之间的高度差(中心点在底部),56 是它们中心的 X 偏移之间的差。

请记住 - 您仍然需要更改中心偏移,因为缩放时会考虑到它。

希望这会有所帮助,我已经为此浪费了几个小时。请记住向 Apple 提交错误报告。

于 2011-03-29T12:24:35.913 回答
0

我认为centerOffset你可以使用setRegionwhich 在所有版本中都可以正常工作。

CGPoint point = [mapView convertCoordinate:selectedAnnotation.coordinate toPointToView:self.view];

CGRect frame = [customView frame];
frame.origin.y = point.y - frame.size.height;
frame.origin.x = point.x - frame.size.width / 2;

MKCoordinateRegion region = [mapView convertRect:frame toRegionFromView:self.view];
[mapView setRegion:region animated:YES];
于 2011-02-28T12:55:55.900 回答