2010-06-09 22 views
11

Estoy trabajando en una aplicación de iPhone basada en UITabBarController y UIViewControllers para cada página. La aplicación necesita para funcionar en modo vertical solamente, por lo que cada delegado controlador de vista + app va con esta línea de código:Detectando la rotación en horizontal manualmente

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

Hay un controlador de vista donde me gustaría para que aparezca un UIImageView cuando el iPhone es rotaed a landscapeleft. El diseño de la imagen se ve en el paisaje, aunque el ancho y la altura son 320x460 (por lo que su retrato).

¿Cómo puedo/debo detectar este tipo de rotación manualmente, solo en este controlador de vista específico, sin tener una rotación automática en toda la vista?

Thomas

ACTUALIZACIÓN:

Gracias!

he añadido este escucha en el viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil]; 

la didRotate se ve así:

- (void) didRotate:(NSNotification *)notification 

    { 
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     if (orientation == UIDeviceOrientationLandscapeLeft) 
     { 
      //your code here 

     } 
    } 

Respuesta

20

que necesitaba que, en un viejo proyecto - espero que todavía funciona ...

1) Registre una notificación:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(detectOrientation) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 

2) A continuación, puede comprobar con respecto a la rotación sobre el cambio:

-(void) detectOrientation { 
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
     ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) { 
     [self doLandscapeThings]; 
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) { 
     [self doPortraitThings]; 
    } 
} 

Espero que ayude!

+3

Gracias! He añadido este oyente en viewDidLoad: [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector (didRotate:) nombre: UIDeviceOrientationDidChangeNotification object: nil]; la didRotate se parece a esto: - (void) didRotate: (NSNotification *) notificación {\t \t orientación UIDeviceOrientation = [[UIDevice currentDevice] orientación]; \t \t si (== orientación UIDeviceOrientationLandscapeLeft) \t { \t \t // su código aquí \t}} –

+1

mejora menor, utilice el UIDeviceOrientationDidChangeNotification constante en lugar de su valor de cadena (que es @ "UIDeviceOrientationDidChangeNotification") –

+0

No te olvides de registrarte como oyente para el evento ... – aroth

2

mejor código de respuesta cómico sin estaría por debajo .. Su código no siempre se disparará correctamente (sólo el 80% de las veces en mis pruebas)

-(void) detectOrientation { 
    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { 
     [self setupForLandscape]; 
    } else if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { 
     [self setupForPortrait]; 
    } 
} 
Cuestiones relacionadas