活动指示器隐藏无效

时间:2023-01-28 22:40:19

i want hide and start the activity indicator every time the user tap on top of the mkannotationPinView.

我希望每次用户点击mkannotationPinView时隐藏并启动活动指示器。

this is my code:

这是我的代码:

-(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];
}

i have try also to switch [self.mapView addSubview:self.activityIndicator];with [self.view addSubview:self.activityIndicator];but the effect is the same, notting appare on the view. whats the problem??? thanks

我也尝试用[self.view addSubview:self.activityIndi​​cator]切换[self.mapView addSubview:self.activityIndi​​cator];但效果是一样的,在视图上注明了。有什么问题???谢谢

3 个解决方案

#1


2  

Queue code after StartAnimating on Main thread again. You can use

在主线程上再次启动StartAnima之后的队列代码。您可以使用

  • dispath_asyn
  • PerformSelectorOnMainThread
  • PerformSelectorInBackground

All three should work just fine.

这三个应该工作得很好。

    -(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];
          }
        });
    }

Explanation: If you do to UI operation without queuing up the operations in main thread effect wont be visible because UI wont show any activity until and unless you finish running the thread and move to next operating in main thread. Same is valid for addSubview removeSubview too.

说明:如果在不排队主线程效果的操作的情况下进行UI操作将不可见,因为UI除非您完成运行线程并移至下一个主线程操作,否则不会显示任何活动。同样适用于addSubview removeSubview。

#2


1  

I don't know if this can solve your problem but I think you're not implementing the activity indicator properly. The activity indicator is used to be shown while things are being downloaded/updated/whatever in background. So.. Your code has to be something like this:

我不知道这是否可以解决您的问题,但我认为您没有正确实施活动指标。活动指示器用于在下载/更新/在后台进行任何操作时显示。所以..你的代码必须是这样的:

-(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];
}

Hope it helped!

希望它有所帮助!

#3


0  

  1. Be sure that you are allocated your activity indicator
  2. 确保为您分配了活动指示器

  3. If it so - you can't see it because hidesWhenStopped property of activity indicator by default is set to YES. Your code will be executed very fast and you wont even see activity indicator
  4. 如果是这样的话 - 您无法看到它,因为默认情况下hidesWhenStopped属性活动指示器设置为YES。您的代码执行速度非常快,您甚至看不到活动指示符

#1


2  

Queue code after StartAnimating on Main thread again. You can use

在主线程上再次启动StartAnima之后的队列代码。您可以使用

  • dispath_asyn
  • PerformSelectorOnMainThread
  • PerformSelectorInBackground

All three should work just fine.

这三个应该工作得很好。

    -(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];
          }
        });
    }

Explanation: If you do to UI operation without queuing up the operations in main thread effect wont be visible because UI wont show any activity until and unless you finish running the thread and move to next operating in main thread. Same is valid for addSubview removeSubview too.

说明:如果在不排队主线程效果的操作的情况下进行UI操作将不可见,因为UI除非您完成运行线程并移至下一个主线程操作,否则不会显示任何活动。同样适用于addSubview removeSubview。

#2


1  

I don't know if this can solve your problem but I think you're not implementing the activity indicator properly. The activity indicator is used to be shown while things are being downloaded/updated/whatever in background. So.. Your code has to be something like this:

我不知道这是否可以解决您的问题,但我认为您没有正确实施活动指标。活动指示器用于在下载/更新/在后台进行任何操作时显示。所以..你的代码必须是这样的:

-(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];
}

Hope it helped!

希望它有所帮助!

#3


0  

  1. Be sure that you are allocated your activity indicator
  2. 确保为您分配了活动指示器

  3. If it so - you can't see it because hidesWhenStopped property of activity indicator by default is set to YES. Your code will be executed very fast and you wont even see activity indicator
  4. 如果是这样的话 - 您无法看到它,因为默认情况下hidesWhenStopped属性活动指示器设置为YES。您的代码执行速度非常快,您甚至看不到活动指示符