如何在视图中显示加载动画 - IOS



我有一个视图,我想在其中显示加载动画。我已经看到一些应用程序,他们正在显示圆形图像以显示加载,并且动作将在背景上发生,我想在这里实现的相同内容,IOS中是否有任何内置动画可用?

蒂亚

您可以使用内置的活动指示器。

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2 , (alert.bounds.size.height) /2);
[indicator startAnimating];

只需将其作为子视图添加到视图中即可。

如果你想

保持简单,你可以使用UIActivityIndicator。或者有很多开源活动指标,除了显示纺车之外,还可以做很多花哨的事情。MBProgressHUD和SVProgressHUD是两个简洁的实现。

创建 YourViewController,然后将 MBProgressHUB 库添加到项目中(可以从此处获取库);下载项目并将库移动到项目中。

然后,您可以使用以下代码来完成您的任务:

YourViewController.h

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
@interface YourViewController : UITableViewController <MBProgressHUDDelegate>
{
    MBProgressHUD *hud;
}

YourViewController.m

#import "YourViewController.h"
@interface YourViewController ()
@end
@implementation YourViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeProgressLoading];
    [self getObjects];
    [hud hide:YES afterDelay:1];
}
-(void) initializeProgressLoading {
    hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:hud];
    hud.delegate = self;
    hud.labelText = @"Loading";
    [hud showWhileExecuting:@selector(sleep) onTarget:self withObject:nil animated:YES];
}
- (void)sleep {
    sleep(50000);
}
- (void) getObjects {
// connect to db and get all objects
//you can write any thing here
}
- (void)hudWasHidden:(MBProgressHUD *)hud1 {
    // Remove HUD from screen when the HUD was hidded
    [hud removeFromSuperview];
    hud = nil;
}

最新更新