Activity 指示器直到加载下一个视图

Activity indicator until load the next view

在我的 firstViewController 中有一个 UIButton (GalleryButton),在我的 secondViewController 中有一个 UITableView。当用户点击 GalleryButton 时,需要 2-3 秒的时间才能打开 secondViewController 并加载图像。我想显示 UIActivityIndicator 直到加载 secondViewController。怎么办??

您应该在后台线程中加载图像并在主线程中显示 UIActivityIndicator。我已经在这里回复了类似的问题:

// Main thread by default: 
// show progress bar here.

DispatchQueue.global(qos: .background).async {
    // Background thread:
    // start loading your images here

    DispatchQueue.main.async {
        // Main thread, called after the previous code:
        // hide your progress bar here
    }
}

在您的第二个视图控制器中以编程方式创建 Activity 指标

    var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

在第二个视图控制器的 viewDidLoad() 中添加以下代码

   activityIndicator.hidesWhenStopped = true
   activityIndicator.center = view.center
   activityIndicator.startAnimating() //For Start Activity Indicator

当数据被填充时 Table 查看完全比添加下面的代码停止 Activity 指标

   activityIndicator.stopAnimating() //For Stop Activity Indicator

这对我有用

#import "ViewController.h"
#import "NextVC.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *aiStart;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.aiStart.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.aiStart.hidden = YES;
    [self.aiStart stopAnimating];
}

- (IBAction)btnShowNextVCTapped:(id)sender {
    dispatch_async(dispatch_get_main_queue(), ^{

        self.aiStart.alpha = 0;
        self.aiStart.hidden = NO;
        [self.aiStart startAnimating];

        [UIView animateWithDuration:0.3 animations:^{
            self.aiStart.alpha = 1;
        } completion:^(BOOL finished) {
            NextVC* nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextVC"];

            [self presentViewController:nextVC animated:YES completion:nil];
        }];
    });


}