Pergunta

Eu tenho procurado muito sobre isso e não consigo encontrar nada para me ajudar.

Eu tenho um UIViewController contido em outro uiviewController. Quando o pai uiviewcontroller gira, digamos do retrato para o paisagismo, quero fazer parecer que a criança não gira. Isto é. Quero que a criança tenha a mesma orientação para o céu, independentemente da orientação dos pais. Se possui um UIBUBILTTON que está na vertical em retrato, eu quero que o lado direito do botão seja "para cima" no UiInterfaceRientationlandscapeleft.

Isso é possível? Atualmente, estou fazendo coisas realmente grosseiras assim:

-(void) rotate:(UIInterfaceOrientation)fromOrientation: toOr:(UIInterfaceOrientation)toOrientation
{
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeRight))
       || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
       || ((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortrait)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeLeft))
       || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeRight)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
       || ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortrait)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
       || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationPortrait)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationLandscapeRight))
       || ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
    {

    }   
}

O que parece ser um desperdício de código perfeitamente bom. Além disso, eu estava pensando em usar o CGAFFINETRANSFORM (como citado aqui: http://www.crystalminds.nl/?p=1102) Mas estou confuso sobre se devo alterar as dimensões da visão para corresponder ao que elas serão após a rotação.

O grande pesadelo aqui é que você precisa acompanhar uma variável global de "orientação". Caso contrário, a ilusão é perdida e o ViewController é girado para qualquer coisa.

Eu realmente poderia usar alguma ajuda nisso, obrigado!

Foi útil?

Solução

A melhor coisa que você pode fazer é alterar os quadros de seus quadros subview de acordo com as orientações da sua interface. Você pode fazer isso como:

 #pragma mark -
 #pragma mark InterfaceOrientationMethods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
    }
    else{   
        //change the view and subview  frames for the landscape view
    }

    [UIView commitAnimations];
}

Espero que isto ajude.

Outras dicas

Eu percebi algo .. digamos que nosso projeto tenha várias camadas de viewcontroller (como em se você adicionar subview de outro controlador de exibição ao seu controlador de exibição)

WillrotateTointerfaceRientation: o método de duração não será chamado para a 2ª camada do ViewController ...

Então, o que eu fiz foi, depois de inicializar meu controlador de exibição de segunda camada da minha camada superior, então, quando o método de duração: o método de duração é chamado na camada mais importante, chamarei WillrotateTointerFaceorentação: duração para o controlador de exibição da 2ª camada também

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top