2010-06-09 19 views
11

Ich arbeite an einer iPhone-Anwendung, die auf UITabBarController und UIViewControllern für jede Seite basiert. Die App muss nur im Portrait-Modus laufen zu lassen, so dass jeder View-Controller + AppDelegate geht mit dieser Codezeile:Manuelle Rotationserkennung

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

Es gibt einen View-Controller, wo Ich mag würde einen UIImageView um Pop-up, wenn das iPhone ist rotaed zu landschaftsfrei. Das Design des Bildes sieht wie eine Landschaft aus, obwohl die Breite und Höhe 320x460 (also das Hochformat) betragen.

Wie kann/sollte ich diese Art der Drehung manuell erkennen, nur in diesem speziellen Ansichtscontroller, ohne eine automatische Drehung auf der gesamten Ansicht?

Thomas

UPDATE:

Dank!

Ich habe diesen Zuhörer in der viewDidLoad:

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

die didRotate wie folgt aussieht:

- (void) didRotate:(NSNotification *)notification 

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

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

     } 
    } 

Antwort

20

ich, dass in einem alten Projekt benötigt - hoffen, dass es funktioniert immer noch ...

1) Registrieren Sie eine Benachrichtigung:

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

2) können Sie die Drehung auf Änderung testen gegen:

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

Hoffnung, das hilft!

+3

Dank! I hinzugefügt, um diesen Zuhörer im viewDidLoad: [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector (didRotate:) Name: UIDeviceOrientationDidChangeNotification Objekt: nil]; die didRotate sieht wie folgt aus: - (void) didRotate: (NSNotification *) Benachrichtigung {\t \t UIDeviceOrientation Orientierung = [[UIDevice currentDevice] Orientierung]; \t \t if (Orientierung == UIDeviceOrientationLandscapeLeft) \t { \t \t // Ihr Code hier \t} } –

+1

Minor Verbesserung, verwenden Sie die Konstante UIDeviceOrientationDidChangeNotification statt dessen String-Wert (das ist @ "UIDeviceOrientationDidChangeNotification") –

+0

Vergessen Sie nicht, sich als Zuhörer für das Event abzumelden ... – aroth

2

besserer Code zu Comic Sans Antwort würde unten sein .. Sein Code wird nicht immer richtig abfeuern (nur 80% der Zeit in meinem Test)

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