更新:迈克尔做对了。这是我的解决方案:
- (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 行,代码可以正常工作,但我只看到最后一个注释,因为它通过了所有其他注释。