2009-05-05 18 views
13

¿Cómo crear ese tipo de vista emergente modal negro/gris que usan muchas aplicaciones, cuando una operación larga pendiente está en progreso?Crear una vista de "carga ..." usando el SDK de iPhone

Al igual que al usar los servicios basados ​​en la localización, la carga de una página web, la pantalla se pone oscuro y hay una vista modal que muestra un icono de giro "Por favor, espere ..."

ejemplo en la siguiente captura de pantalla:

Screenshot

Respuesta

14

Si desea evitar la API no documentada, también puede consultar MBProgressHUD. Es similar a UIProgressHUD e incluso tiene algunas características adicionales.

+0

URL da un 404? –

+2

Perdón por eso. Consíguelo en http://github.com/matej/MBProgressHUD –

17

Esto es realmente el UIProgressHUD indocumentado (en 2.2.1 de todos modos). Crea uno como este:

En su .h:

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

En su .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

¿Se puede utilizar y enviar a la App Store? –

+3

Sí, esto no está vinculando a la API privada. Hemos hecho algo similar con los botones en UIAlertView, revisamos sin problemas. – zoul

+0

No lo sabía - gracias zoul. –

4

Si agrega un UIView como una vista secundaria de la ventana principal que cubrirá la totalidad UI. Hazlo parcialmente transparente y parcialmente translúcido y se verá como una ventana emergente.

Este ejemplo muestra cómo fade the Default.png splash screen, comenzando con que es bastante sencillo agregar un par de métodos al delegado de su aplicación (que tiene un puntero a la ventana principal) para presentar y descartar la vista de progreso.

+0

Hola, duncanwilcox, yo quería esto también ... desapareciendo la pantalla default.png ... :) Gracias ... Parece que respondes preguntas que todavía no he preguntado ...: ¿D – Mugunth

4

Eche un vistazo a la aplicación para iPhone de Wordpress (http://iphone.wordpress.org/) para ver un ejemplo de cómo hacerlo sin utilizar ninguna API no documentada.

1

Yo uso LoadingHUDView para este propósito, y funciona siempre.

obtener LoadingHUDView.m y LoadingHUDView.h y haga lo siguiente en su clase base (o lo que sea)

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

espero que esto ayude. agradecimiento

+0

Puede usarlo como: \t [self showModalActivityIndicator: @ "Iniciando sesión ..."]; – thesummersign

11

Creo que los más simples (unas pocas líneas de código), perfectamente documentados y más bella forma de hacerlo es utilizar la UIAlertView con un UIActivityIndicatorView:

http://iosdevelopertips.com/user-interface/uialertview-without-buttons-please-wait-dialog.html

UIAlertView with UIActivityIndicatorView http://iosdevelopertips.com/wp-content/uploads/2010/02/alert3-204x300.png

+0

Esta fue una gran solución ... pero ya no parece funcionar con iOS 7. – HughHughTeotl

0

En XI ter archivo, coloque UIView y UIActivityIndicatorView. Establecer ActivityIndicatorView Property SetAnimado a Sí.

@property (retener, no atómico) IBOutlet UIView * m_LoadingView;

Mientras partir operaciones largas, agregue la LoadingView a la vista

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

Después de completar el proceso, Retire LoadingView de super vista. [m_LoadingView removeFromSuperView];

Cuestiones relacionadas