Как нажать на полноэкранный контроллер, когда iPhone поворачивается? - PullRequest
1 голос
/ 20 марта 2012

Я хотел бы показать другой полноэкранный вид, когда устройство iOS повернуто в горизонтальную ориентацию, и вернуться к предыдущему виду, когда устройство повернуто обратно в альбомную.

Я в основном заставил его работать, используя один контроллер представления и два представления, а затем установил self.view контроллера представления в - shouldAutorotateToInterfaceOrientation: в соответствующее представление.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){

        self.view = landscapeView;

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
          (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){

        self.view = portraintView;

    }
    return YES;
}

Тем не менее, в идеале я хотел бы, чтобы ландшафтный вид имел свой отдельный контроллер представления для управления видом.Я попытался выдвинуть контроллер представления модально и отклонить его в shouldAutorotateToInterfaceOrientation:, но контроллер горизонтального представления не появляется в правильной ориентации (он все еще думает, что устройство в портретной ориентации)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){

        [self presentModalViewController:landscapeViewController animated:YES];

    }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
          (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){

        [self dismissModalViewControllerAnimated:YES];

    }
    return YES;
}

Затем, когда я возвращаюсь к портретному изображению, он все еще думает, что устройство находится в альбомной ориентации.

Ответы [ 2 ]

1 голос
/ 20 марта 2012

Вы должны выполнять свою ротацию в willAnimateRotationToInterfaceOrientation: duration: или didRotateToInterfaceOrientation:, а не shouldRotateToInterfaceOrientation.Затем используйте прилагаемый interfaceOrientation, чтобы переключить свои взгляды.Этот способ намного надежнее и вызывается только при вращении устройства.

0 голосов
/ 20 марта 2012

Как указал @MishieMoo, мне нужно было выполнить свою работу в didRotateToInterfaceOrientation, чтобы контроллер представления отображался в правильной ориентации.

Так что теперь код для моего контроллера портретного представления выглядит следующим образом:

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

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if(fromInterfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationPortraitUpsideDown == UIInterfaceOrientationLandscapeRight){
        [self performSegueWithIdentifier:@"fullscreenSegue" sender:self];
    }
}

Я запускаю раскадровку, чтобы выдвинуть контроллер полноэкранного просмотра, но вы также можете легко загрузить контроллер представления и выполнить [self presentModalViewController: landscapeViewController animated: YES].

Икод для отклонения вида в полноэкранном контроллере:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{    
    if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        [self dismissModalViewControllerAnimated:NO];
    }
}
...