如何设置MKAnnotationView的旋转中心

9

当我在MKMapView启用MKUserTrackingModeFollowWithHeading时,我的MKAnnotationView存在问题。

我使用MKAnnotationView的centerOffset属性来定位我的图像。指定针尖相对于图像中心的坐标有些不直观,但我想出了以下公式:

annotationView.centerOffset = CGPointMake(imageWidth/2.0 - tipXCoordinate, imageHeight/2.0 - tipYCordinate);

这对于缩放地图的大小没有问题,标记针的顶部在地图上保持相对位置。
然而,当我启用MKUserTrackingModeFollowWithHeading时,它就不能正常工作了。标记针围绕图像中心旋转,而不是围绕其顶部。因此,当地图旋转时,标记针的顶部并未指向它们应该注释的位置。
我已经尝试过一些MKAnnotationViewframecenter属性,但感觉它们对针的对齐没有任何影响。
有趣的是,MKPinAnnotationView似乎根本不使用centerOffset,而是使用了一个偏移的frame。然而,我无法复制这个效果。改变自定义视图的frame根本没有移动它。
谢谢您提供的任何见解 :-)
解决方案:
不要使用centerOffset!使用annotationView.layer.anchorPoint代替。锚点的坐标系也更好。坐标范围从图像矩形的0.0(左上角)到1.0(右下角):
annotationView.layer.anchorPoint = CGPointMake(tipXCoordinate/imageWidth, tipYCordinate/imageHeight);
1个回答

13

我的朋友让我告诉你,你应该“试试这个实例”:

self.layer.anchorPoint = CGPointMake (0.5f, 1.0f);

1
告诉你的朋友,他/她是我本周的个人英雄 :-) 非常感谢! - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接