2012-08-14 28 views
4

我有,我想顯示加載動畫視圖。我已經看到了一些應用程序顯示,他們是圓形圖像顯示加載,並且動作將發生在後臺,我想在這裏實現同樣的事情,任何內置的動畫可在IOS?如何顯示加載動畫視圖 - IOS

TIA

+4

您可以簡單地使用UIActivityIndi​​cator是你只是想顯示一個加載過程... – IronManGill 2012-08-14 06:53:11

回答

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

+1

你可以d ownload教程從here.http://bit.ly/Rc3azb – 2012-08-14 06:56:57

+0

從https://github.com/jdg/MBProgressHUD獲取圖書館。有兩行你需要,一個打開它,另一個隱藏。在git hub上向下滾動,你會看到代碼行。 – 2013-09-26 11:43:53

3

,如果你想使事情變得簡單,您可以使用UIActivityIndi​​cator。或者有很多開源活動指標,除了展示紡車之外,還有很多花哨的東西。 MBProgressHUDSVProgressHUD是兩個很好的實現。

8

您可以使用內置的活動指示燈。

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

只需將它作爲子視圖添加到您的視圖中即可。

+0

很好的回答。我建議添加如下內容:[self.view addSubview:indicator];即使你說要「簡單地將其添加爲一個子視圖到您的視圖」 – helloGo 2016-12-21 21:22:33

+0

@helloGo我更願意把它作爲一個評論,一個可能想不直接添加一個指標到'self.view'一些其他觀點或子視圖 – ColdSteel 2016-12-25 15:53:59

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