开发者

How to keep image from autorotating?

I've got an UIImageView as a background image for my application, which, like a helicopter in distress, autorotates. The problem is when the orientati开发者_如何学JAVAon 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.


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];
}


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"];

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜