Поддержка ориентации раскадровки для xCode 4.2?

Я обновился до xCode 4.2, и это новая функция раскадровки. Однако не смог найти способ поддерживать как портрет, так и пейзаж.

Конечно, я сделал это программно, с двумя видами, одно для портрета и одно для пейзажа, как в старые времена, и:

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    {
        self.view = self.landscapeView;
    }
    else
    {
        self.view = self.portraitView;
    }

Но я искал способ как-то сделать это автоматически. То есть сейчас это xCode 4.2, я ожидал от него большего. Спасибо всем.

==================================
ВРЕМЕННОЕ РЕШЕНИЕ:

Я представлю здесь временное решение. Я говорю, что это временно, потому что я все еще жду, что ребята из Apple сделают что-то действительно умное в этом отношении.

Я создал еще один файл .storyboard, назвав его «MainStoryboard_iPhone_Landscape», и реализовал там контроллеры альбомной ориентации. На самом деле, это точно так же, как и обычный (портретный) .storyboard, но все экраны в альбомном режиме.

Итак, я извлечу ViewController из ландшафтной раскадровки, а когда произойдет вращение, просто заменим self.view новым представлением viewController.

1. Генерировать уведомления при изменении ориентации:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

2. Искать уведомления:

[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {    
    // We must add a delay here, otherwise we'll swap in the new view  
    // too quickly and we'll get an animation glitch  
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}];

3. Реализовать updateLandscapeView

- (void)updateLandscapeView {  
 //>     isShowingLandscapeView is declared in AppDelegate, so you won't need to declare it in each ViewController
 UIDeviceOrientation deviceOrientation       = [UIDevice currentDevice].orientation;
 if (UIDeviceOrientationIsLandscape(deviceOrientation) && !appDelegate().isShowingLandscapeView)
 {
     UIStoryboard *storyboard                = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_Landscape" bundle:[NSBundle mainBundle]];
     MDBLogin *loginVC_landscape             =  [storyboard instantiateViewControllerWithIdentifier:@"MDBLogin"];
     appDelegate().isShowingLandscapeView    = YES;  
     [UIView transitionWithView:loginVC_landscape.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
         //>     Setup self.view to be the landscape view
         self.view = loginVC_landscape.view;
     } completion:NULL];
 }
 else if (UIDeviceOrientationIsPortrait(deviceOrientation) && appDelegate().isShowingLandscapeView)
 {
     UIStoryboard *storyboard                = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
     MDBLogin *loginVC                       = [storyboard instantiateViewControllerWithIdentifier:@"MDBLogin"];
     appDelegate().isShowingLandscapeView    = NO;
     [UIView transitionWithView:loginVC.view duration:0 options:UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseIn animations:^{
         //>     Setup self.view to be now the previous portrait view
         self.view = loginVC.view;
     } completion:NULL];
 }}

Всем удачи.

P.S: Я приму ответ Эда Тейлора, потому что после долгого ожидания и поиска решения я закончил реализацию чего-то, вдохновленного его ответом. Спасибо, Тейлор.

13
задан Beny Boariu 26 June 2012 в 12:45
поделиться