2
更新:邁克爾說得沒錯。這裏是我的解決方案:MKAnnotationView顯示沒有文字的標註
- (void) connectNextCarOnMainThread:(id)annotation{
[self performSelectorOnMainThread:@selector(connectNextCar:) withObject:annotation waitUntilDone:YES];
}
- (void) connectNextCar:(id)annotation{
Pin *pin = (Pin *)annotation;
MKMapRect zoomRect = MKMapRectNull;
MKMapPoint annotationPoint = MKMapPointForCoordinate(pin.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 3, 3);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[mapView setVisibleMapRect:zoomRect animated:YES];
[mapView selectAnnotation:pin animated:YES];
NSInteger currentIndex=[self.annotations indexOfObject:annotation];
if(currentIndex < [self.annotations count]){
[self performSelector:@selector(connectNextCarOnMainThread:) withObject:[self.annotations objectAtIndex:currentIndex+1] afterDelay:5];
}
}
我想實現一個簡單的功能:中心並選擇每X秒我的註解之一。但是我在註釋標註中出現了一些奇怪的行爲。
這裏是我的代碼:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if(self.movedToFitPins){
for(id <MKAnnotation> pin in self.annotations)
[self.mapView addAnnotation:pin];
self.movedToFitPins = NO;
[self performSelectorInBackground:@selector(fakeCarConnections) withObject:nil];
}
}
- (void) fakeCarConnections {
for (Pin *annotation in self.annotations)
{
[NSThread sleepForTimeInterval : 10.0];
MKMapRect zoomRect = MKMapRectNull;
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 3, 3);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[mapView setVisibleMapRect:zoomRect animated:YES];
[mapView selectAnnotation:annotation animated:YES];
}
}
那麼,所發生的事情是,我專注於詮釋中,調出泡不開,但沒有文字裏。如果我在註釋中單擊,標註將正確打開文本。
這裏有一個問題:如果我評論sleepForTimeInterval這一行,代碼工作正常,但我只能看到最後一個註釋,因爲它傳遞了所有其他註釋。
工作,謝謝。我用我的解決方案更新了我的問題。 =) – decomush
你應該更新你的'performSelector'調用來明確使用主線程,因爲你正在更新UI。如果沒有這個,可能會讓自己失敗。這是一個痛苦,因爲你不能將延遲傳遞給'performSelectorOnMainThread',我知道 - 但可能會在以後節省一個調試噩夢。 –
所以我應該讓我的'performSelector:withObject:afterDelay:'調用一個選擇器,它所做的就是使用'performSelectorOnMainThread'調用另一個選擇器? – decomush