2009-05-05 79 views
13

如何創建黑色/灰色模式彈出式視圖,當許多應用程序使用,當一些長時間掛起操作正在進行?使用iPhone SDK創建「加載...」視圖

喜歡使用基於位置的服務,加載網頁,屏幕會變暗並沒有呈現出紡紗圖標模式的看法時,「請稍候...」下面的屏幕截圖

例子:

Screenshot

回答

14

如果你想避免無證的API,你也可以看看MBProgressHUD。它與UIProgressHUD類似,甚至還有一些附加功能。

17

這實際上是無證的(在2.2.1中)UIProgressHUD。創建一個這樣的:

在你的.h:

@interface UIProgressHUD : NSObject 
- (UIProgressHUD *) initWithWindow: (UIView*)aWindow; 
- (void) show: (BOOL)aShow; 
- (void) setText: (NSString*)aText; 
@end 

在您的m:

- (void) killHUD: (id)aHUD 
{ 
[aHUD show:NO]; 
[aHUD release]; 
} 

- (void) presentSheet 
{ 
id HUD = [[UIProgressHUD alloc] initWithWindow:[contentView superview]]; 
[HUD setText:@"Doing something slow. Please wait."]; 
[HUD show:YES]; 
[self performSelector:@selector(killHUD:) withObject:HUD afterDelay:5.0]; 
} 
+3

這可以使用並提交到App Store嗎? – 2009-05-05 05:27:28

+3

是的,這沒有鏈接到私有API。我們用UIAlertView中的按鈕做了類似的事情,經過審查沒有任何問題。 – zoul 2009-05-05 08:30:12

+0

我不知道 - 謝謝祖爾。 – 2009-05-05 09:58:56

4

如果添加一個UIView,因爲它會覆蓋整個主窗口的子視圖UI。使其部分透明,部分半透明,看起來像一個彈出。

這個例子展示瞭如何從fade the Default.png splash screen開始,它非常簡單地向您的應用程序委託(具有指向主窗口的指針)添加一些方法來呈現和取消進度視圖。

1

我使用LoadingHUDView爲此目的,它始終工作。

得到LoadingHUDView.mLoadingHUDView.h,做在你的基類(或其他)

#pragma mark ActivityIndicator Methods 

-(void) showModalActivityIndicator:(NSString *)message 
{ 
     loadingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]retain];// origional 
     //loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; //testing 
     loadingView.backgroundColor = [UIColor grayColor]; //[UIColor colorWithRed:1 green:1 blue:1 alpha:0.3]; 
     LoadingHUDView *loadHud = [[LoadingHUDView alloc] initWithTitle:message]; 
     loadHud.center = CGPointMake(160, 290); 
     [loadingView addSubview:loadHud]; 
     [loadHud startAnimating]; 
     [loadHud release]; 
     [loadingView setAlpha:0.0]; 
      [self.tableView addSubview:loadingView]; 
     [UIView beginAnimations:@"fadeOutSync" context:NULL]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:0.5]; 
     [loadingView setAlpha:0.5]; 
     [UIView commitAnimations]; 
} 

-(void) hideModalActivityIndicator { 
    if (loadingView) { 
    [UIView beginAnimations:@"fadeOutSync" context:NULL]; 
    [UIView setAnimationDidStopSelector:@selector (removeTranparentView) ]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.5]; 
    [loadingView setAlpha:0]; 
    [UIView commitAnimations]; 
     } 
} 

-(void)removeTranparentView 
{ 
    [loadingView removeFromSuperview]; 
    [loadingView release]; 
    loadingView = nil; 
} 

希望這有助於以下。 謝謝

0

在XIB文件,放置UIView和UIActivityIndi​​catorView。 將ActivityIndi​​catorView屬性SetAnimated設置爲Yes。

@property(retain,nonatomic)IBOutlet UIView * m_LoadingView;

同時開始長操作,LoadingView添加到視圖

m_LoadingView.layer.cornerRadius = 10; 
m_LoadingView.center = self.view.center; 
[self.view addSubview:m_LoadingView]; 

完成過程後,從超級視圖中移除LoadingView。 [m_LoadingView removeFromSuperView];