2011-01-21 12 views
12

nella mia app voglio chiamare un metodo in CCScene myscene in caso di rotazione del dispositivo (cambio di orientamento). Ho disabilitato l'autorotazione (perché voglio che non accada).Come iscriversi autonomamente all'evento di Orientamento dispositivo (non l'orientamento dell'interfaccia)?

Il problema è: voglio cambiare la gravità nella scena a seconda dell'orientamento del mio dispositivo. Il mio codice:

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

Ma in questo caso posso ottenere le notifiche solo in caso di autorotazione abilitato (se disattivato, il dispositivo è in realtà non cambiano lo stato di bar orientamento) mi potete aiutare.?

risposta

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

È necessario verificare la presenza di non Orientamento del dispositivo barra di stato.

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

Aggiunto mancante '['. – Macarse

+0

L'uso dell'orientamento del dispositivo è troppo sensibile, cambierà in verticale quando è orizzontale a 45 gradi. – danfordham

+0

Ecco perché si chiama ** orientamento del dispositivo ** e non ** orientamento dell'interfaccia **. Un dispositivo mobile può cambiare molto spesso il proprio orientamento in base al modo in cui viene tenuto. Le persone interessate all'orientamento dell'interfaccia devono utilizzare la proprietà di UIViewController :) – nacho4d

Problemi correlati