Question

I've got an UIImageView as a background image for my application, which, like a helicopter in distress, autorotates. The problem is when the orientation autorotates my image gets shifted all over the place and I don't like it. Since it's a simple texture I figure the user would prefer to have it stay in its place, and not autorotate. What I mean by this is that I would like the image to stay full screen, and when the user rotates the phone the image is just rotated 90 degrees so it appears the image is static with the phone, not the rest of the application.

Was it helpful?

Solution

You can try implementing the willAnimateRotationToInterfaceOrientation:duration: method in your view controller and rotating the UIImageView in the opposite direction.

The code below is from the top of my head, I can't guarantee that it will work:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
    CGFloat angle = M_PI/2; // Figure out the proper angle
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    imageview.transform = CGAffineTransformMakeRotation(angle);
    [UIView commitAnimations];
}

OTHER TIPS

I suggest to rotate your image in an image-editing-software and load it dynamically. This is far, far easier to do, than messing around with the angles (and also the code is easier to read).

Like this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

        myBackground.image = [UIImage imageNamed:@"bg.jpg"];            

    } else { // landscape view      

        myBackground.image = [UIImage imageNamed:@"bg_landscape.jpg"];

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top