我希望每次用户点击mkannotationPinView时隐藏并启动活动指示器。
这是我的代码:
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (!self.activityIndicator) {
NSLog(@"error");
}
self.activityIndicator.hidden = NO;
[self.mapView addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
if ([view.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)(view.leftCalloutAccessoryView);
if ([view.annotation respondsToSelector:@selector(thumbnail)]) {
imageView.image = [view.annotation performSelector:@selector(thumbnail)];
}
}
[self.activityIndicator stopAnimating];
}
我还尝试将[self.mapView addSubview:self.activityIndicator];
切换为[self.view addSubview:self.activityIndicator];
,但效果是一样的,在视图上注明了。有什么问题???
谢谢
答案 0 :(得分:2)
再次在主线程上的StartAnimating之后的队列代码。你可以使用
这三个都应该可以正常工作。
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (!self.activityIndicator) {
NSLog(@"error");
}
self.activityIndicator.hidden = NO;
[self.mapView addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
dispatch_async(dispatch_get_main_queue(), ^(void) {
if ([view.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)(view.leftCalloutAccessoryView);
if ([view.annotation respondsToSelector:@selector(thumbnail)]) {
imageView.image = [view.annotation performSelector:@selector(thumbnail)];
}
[self.activityIndicator stopAnimating];
}
});
}
说明:如果你在没有排队主线程效果的操作的情况下进行UI操作将不可见,因为UI除非你完成运行线程并转到下一个在主线程中运行,否则不会显示任何活动。同样适用于addSubview removeSubview。
答案 1 :(得分:1)
我不知道这是否可以解决您的问题,但我认为您没有正确实施活动指标。活动指示器用于在下载/更新/在后台进行任何操作时显示。所以..你的代码必须是这样的:
-(void)methodThatShowsTheActivityIndicator{
self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];
[self performSelectorInBackground:@selector(backgroundProcess) withObject:self];
}
-(void)backgroundProcess{
//some process
[self performSelectorOnMainThread:@selector(finishLoad) withObject:self waitUntilDone:false];
}
-(void)finishLoad{
self.activityIndicator.hidden = YES;
[self.activityIndicator stopAnimating];
}
希望它有所帮助!
答案 2 :(得分:0)