Pier Pier - 6 months ago 26
iOS Question

iphone background images when changing device rotation

I have an UIImageView as a background behind my controls, the problem comes when rotating the device. I'd like to use a different image depending on the orientation.

So what would be the best way to handle that?
Adding and removing each UIImageView for landscape and portrait?
Having both on the view and hide/show the appropriate one?
Or maybe there is some class or UI object for handling that?

TIA

Answer

You can get funky with it and have both background images overlaid, one with alpha = 1.0 and the other with alpha = 0.0. Then, when you rotation, use an animation to transition the alphas to 0.0 and 1.0, respectively to get a fade-in fade-out effect. If you choose this approach, I recommend putting the animation code in -viewWillAppear: For example, something like this:

[UIView beginAnimations:nil context:NULL]; {
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
        [UIView setAnimationDelegate:self];
        if (toOrientation == UIInterfaceOrientationLandscape) {
                landscapeView.alpha = 1.0;
                portraitView.alpha = 0.0;
        }
        else {
                landscapeView.alpha = 0.0;
                portraitView.alpha = 1.0;
        }
} [UIView commitAnimations];
Comments