2011-01-21 14 views
12

en mi aplicación quiero llamar a algún método en CCScene myscene en el caso de la rotación del dispositivo (cambio de orientación). Inhabilité la autorrotación (porque no quiero que suceda).¿Cómo suscribirse a sí mismo en caso de orientación del dispositivo (no orientación de la interfaz)?

El problema es: quiero cambiar la gravedad de la escena según la orientación de mi dispositivo. Mi código:

-(void) onEnter 
{ 
    [super onEnter]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
} 
-(void) onExit 
{ 
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil]; 
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
} 
-(void)notification_OrientationWillChange:(NSNotification*)n 
{ 
    orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue]; 
} 
-(void)notification_OrientationDidChange:(NSNotification*)n 
{ 
    if (orientation == UIInterfaceOrientationLandscapeLeft) { 
     b2Vec2 gravity(0, -10); 
     world->SetGravity(gravity); 
    } 
    if (orientation == UIInterfaceOrientationLandscapeRight) { 
     b2Vec2 gravity(0, 10); 
     world->SetGravity(gravity); 
    } 
} 

Pero en este caso puedo obtener notificaciones sólo en el caso de auto rotación permitido (si es desactivada, el dispositivo es en realidad no lo cambia el estado de orientación bar) ¿Me puede ayudar.?

Respuesta

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



- (void)orientationChanged:(NSNotification *)notification{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    //do stuff 
    NSLog(@"Orientation changed");   
} 

Debe comprobar la orientación del dispositivo, no la barra de estado.

typedef enum { 
     UIDeviceOrientationUnknown, 
     UIDeviceOrientationPortrait,   // Device oriented vertically, home button on the bottom 
     UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top 
     UIDeviceOrientationLandscapeLeft,  // Device oriented horizontally, home button on the right 
     UIDeviceOrientationLandscapeRight,  // Device oriented horizontally, home button on the left 
     UIDeviceOrientationFaceUp,    // Device oriented flat, face up 
     UIDeviceOrientationFaceDown    // Device oriented flat, face down 
    } UIDeviceOrientation; 
+1

Agregado faltante '['. – Macarse

+0

El uso de la orientación del dispositivo es demasiado sensible, cambiará a vertical cuando se encuentre a 45 grados del paisaje. – danfordham

+0

Es por eso que se llama ** orientación de dispositivo ** y no ** orientación de interfaz **. Un dispositivo móvil puede cambiar su orientación muy frecuentemente según la forma en que lo tenga. Las personas interesadas en la orientación de la interfaz deben usar la propiedad de UIViewController :) – nacho4d

Cuestiones relacionadas