活动指示器,直到加载下一个视图

时间:2017-01-08 07:38:18

标签: ios swift uitableview uiactivityindicatorview

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

3 个答案:

答案 0 :(得分:2)

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

// 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
    }
}

答案 1 :(得分:1)

在第二个视图控制器中以编程方式创建活动指示器

    var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

在Second View Contoller的viewDidLoad()中添加以下代码

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

在表格视图中填写数据完全比添加以下代码以停止活动指标

   activityIndicator.stopAnimating() //For Stop Activity Indicator

答案 2 :(得分:1)

这对我有用

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


}