2010-04-29 12 views
12

Hola a todos. Tengo una pregunta bastante simple. Estoy desarrollando una aplicación para iPad "rica" ​​y tengo dos imágenes de fondo diseñadas específicamente para paisajes y retratos. Me gustaría que esta ImageView cambie automáticamente según la orientación de los dispositivos. (como casi todas las aplicaciones de Apple iPad).Cambiando UIView en el cambio de orientación

¿Alguien puede indicarme la dirección correcta? Supongo que sería algo que hago en viewDidLoad ..

+0

repetición eventual de http://stackoverflow.com/questions/2489845/rotate-uiviewcontroller-to-counteract-changes-in-uiinterfaceorientation/2490719#2490719 –

Respuesta

22

Lo mejor que puede hacer es cambiar los marcos de sus marcos de subvista de acuerdo con las orientaciones de su interfaz. Puede hacerlo como:

#pragma mark - 
#pragma mark InterfaceOrientationMethods 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation)); 
} 

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

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

    } 
    else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){ 
     //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 esto ayude.

+0

Que hizo el truco, gracias! – Designeveloper

+0

Las macros 'UIInterfaceOrientationIsPortrait()' y 'UIInterfaceOrientationIsLandscape()' realmente aumentan la legibilidad para este tipo de situaciones ... pero, ¡gran solución, no obstante! – Nate

+0

@Nate Gracias por la sugerencia .. Cambió la respuesta. :) –

9

De hecho, descubrí una manera alternativa muy simple de evitar esto. Desde que estoy solo cambiar la imagen de fondo, la adición de este ..

`

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration { 
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == 
     UIInterfaceOrientationPortraitUpsideDown) { 
     [brownBackground setImage:[UIImage imageNamed:@"Portrait_Background.png"]]; 
    } else { 
     [brownBackground setImage:[UIImage imageNamed:@"Landscape_Background.png"]]; 
    } 
} 

`

Cambia el fondo de una UIImageView declarada basada en la orientación. El único inconveniente es que la imagen de fondo actual no está visible en el constructor de Interfaz ya que se maneja con código.

7

Una pequeña adición al enfoque de Madhup, que es genial. Me pareció que tenía que añadir esto a viewDidLoad para configurar la imagen inicial de fondo para retrato o paisaje:

// set background image 
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"portraitBG.png"]]; 
} else { 
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"landscapeBG.png"]]; 
} 

gracias de nuevo Madhup

+0

esta es la forma más correcta –

+0

+1 para 'self.interfaceOrientation' –

0

Usted puede encapsular esta totalmente en su UIView por ver si bounds.width > bounds.height

Este puede ser deseable si está escribiendo un pequeño control autoconsciente.

class MyView: UIView { 
    override func layoutSubviews() { 
    super.layoutSubviews() 
    if bounds.height > bounds.width { 
     println("PORTRAIT. some bounds-impacting event happened") 
    } else { 
     println("LANDSCAPE") 
    } 
    } 
} 
Cuestiones relacionadas