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

时间:2012-08-14 06:51:50

标签: iphone objective-c ios xcode

我有一个视图,我想在其中显示加载动画。我看到一些应用程序显示循环图像以显示加载,动作将在背景上发生,我想在这里实现同样的事情,IOS中是否有任何内置动画?

TIA

4 个答案:

答案 0 :(得分:8)

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

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

只需将其作为子视图添加到您的视图中。

答案 1 :(得分:7)

请使用MBProgressHUD。

#import <UIKit/UIKit.h>;
#import "MBProgressHUD.h" // import the .h file into project.
@class MBProgressHudDemoViewController;

@interface MBProgressHudDemoAppDelegate : NSObject <UIApplicationDelegate,MBProgressHUDDelegate> {
    UIWindow *window;
    MBProgressHudDemoViewController *viewController;
        MBProgressHUD *HUD;// create the object of Hud.
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MBProgressHudDemoViewController *viewController;


-(void)showProgressHUD:(NSString*)msg;
-(void)hideProgressHUD;
@end

#import &lt;UIKit/UIKit.h&gt;
#import "MBProgressHudDemoAppDelegate.h"
@interface MBProgressHudDemoViewController : UIViewController {
    MBProgressHudDemoAppDelegate *delegate;
}

@end

ViewControll.m文件

#import "MBProgressHudDemoViewController.h"
#import "MBProgressHudDemoAppDelegate.h"
@implementation MBProgressHudDemoViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

        [self performSelector:@selector(nextDetail)  withObject:nil afterDelay:2]; 

}
-(void)nextDetail{
    delegate = (MBProgressHudDemoAppDelegate*)[[UIApplication sharedApplication] delegate];
    [delegate showProgressHUD:@"Please Wait..."];
        [self performSelector:@selector(RemoveHud)  withObject:nil afterDelay:2]; 
}
-(void)RemoveHud{
    [delegate hideProgressHUD];
}
@end

<强>输出

enter image description here

答案 2 :(得分:3)

如果您想保持简单,可以使用UIActivityIndi​​cator。或者有很多开源活动指标除了展示纺车之外还有很多花哨的东西。 MBProgressHUDSVProgressHUD是两个简洁的实现。

答案 3 :(得分:1)

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

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

<强> 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;
}