2012-07-13 54 views
2

我在显示当前用户位置的MKMapView上有移动注释。我为该注释设置了自己的图像,我想旋转它以便看到标题。MKAnnotationView旋转

viewForAnnotation委托,我有:

static NSString *planeIdentifier = @"Plane"; 
static NSString *stationIdentifier = @"Station"; 
NSString *identifier; 

if([[annotation title] rangeOfString:@"Plane" options:NSLiteralSearch].location == NSNotFound) { 
    identifier = stationIdentifier; 
} 
else { 
    identifier = planeIdentifier; 
} 

MKAnnotationView *annotationView = (MKAnnotationView *) [aMapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
if (annotationView == nil) { 
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
} else { 
    annotationView.annotation = annotation; 
} 

annotationView.enabled = YES; 

annotationView.canShowCallout = NO; 
    annotationView.image = [UIImage imageNamed:@"airPlane.png"]; 

return annotationView; 

调用实例方法的代码如下:

[planeAnnotation setCoordinate:planeCoordinate]; 
MKAnnotationView* planeView = [self mapView:mapView viewForAnnotation:planeAnnotation]; 
[planeView setTransform:CGAffineTransformMakeRotation([[[self modele] udpData] getValueFor:HDING_TRUE item:DYNAMICS]*(M_PI/180))]; 

的问题是标题从不更新,即图像从不旋转。

任何帮助非常感谢!

编辑:我曾尝试设置为每MKAnnotationView我对MapView的(几百个)的自定义标识符(注释的标题),但我注意到annotationView总是,这意味着它不会实际上从它出列任何东西。我认为这是它不旋转的原因,因为我没有旋转我在地图上显示的相同MKAnnotationView。

+0

“标题”在哪里和何时发生变化?所有数百个注释应该旋转还是只有一个?数百个可以同时显示吗?查看[这个相关的问题](http://stackoverflow.com/questions/6808792/change-uiimage-from-mkannotation-in-the-mkmapview)的答案建议使用'viewForAnnotation' _instance_方法检索当前视图您可以尝试实时转换它,而不是再次删除和添加。 – Anna 2012-07-16 20:30:57

+0

你好安娜,为了回答你的问题,'heading'变量每秒更改20次,它是一个在UDP端口上接收到的值。只有一个注释应该旋转,即显示用户在地图上移动的注释。所有其他人都是固定电台。它们并非全部可见,您需要滚动地图。我试图使用实例方法来转换视图,但没有改变。我认为这个问题可能来自于我的MKAnnotationView没有被正确重用,导致其他(现在隐藏的)视图被编辑。 – bentroc 2012-07-16 20:40:37

+0

我编辑了我的代码,向您显示每个MKAnnotationView现在使用的唯一标识符。在调用实例方法时,行'annotationView.annotation = annotation;'永远不会被调用,显示MKAnnotationView永远不会被重用。 – bentroc 2012-07-16 20:44:29

回答

4

为了回答我的问题,问题在于我调用委托方法 MKAnnotationView* planeView = [self mapView:mapView viewForAnnotation:planeAnnotation];而不是在转换MKAnnotationView时调用实例方法MKAnnotationView* planeView = [mapView viewForAnnotation:planeAnnotation];