2012-07-24 74 views
8

当MKMapView启用MKUserTrackingModeFollowWithHeading时,我遇到了MKAnnotationView的问题。如何设置MKAnnotationView的旋转中心

我使用MKAnnotationView的centerOffset属性定位了我的图像。指定针的针尖相对于在图像的中心坐标系的坐标是有点反intutive,但我想出了下面的公式:

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

这工作正常,在放大和缩小地图。引脚的尖端保持其在地图上的相对位置。

但是,当我启用MKUserTrackingModeFollowWithHeading时,它将不再工作。引脚围绕图像的中心旋转,而不是尖端。所以当地图旋转时,提示并不指向他们应该注释的位置。

我已经玩了framecenter属性MKAnnotationView,但我觉得,他们没有任何影响的针脚alignement。

有趣的是,MKPinAnnotationView似乎根本没有使用centerOffset,而是改用frame代替。但是,我无法重现这一点。更改我的自定义视图的frame根本没有移动它。

感谢任何见解,可以提供:-)

解决方案:

不要使用centerOffset!改为使用annotationView.layer.anchorPoint。 achor点的坐标系也更好。坐标范围为0.0(上/左)到1.0(下/右)的图像矩形为:

annotationView.layer.anchorPoint = CGPointMake(tipXCoordinate/imageWidth, tipYCordinate/imageHeight); 

回答

12

一个朋友问我,让你知道,你应该“试试这个,例如”:

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

告诉你的朋友,(s)他是我本周的个人英雄:-)非常感谢! – Chris 2012-07-25 08:00:25