2012-07-13 49 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];