开发者

Disabling autorotate for a single UIView

Okay, this seems like it should be relatively simple, but I've been Googling for the better 开发者_如何学Pythonpart of an hour, and can't seem to find what I need.

I have a view controller that has a few different parts: a background view, a header view, and a few buttons. Now, I want the header and buttons to autorotate properly (they do, when I return YES from shouldAutorotateToInterfaceOrientation:), but under no circumstances should the background view rotate. Is there a proper way to do this?


If you don't want temporary rotations of the background view after which the background is changed to have the correct orientation, you'll need to turn off Autorotation for the View. No two ways about it.

You would need to handle the rotation of the buttons yourself. You can either make a nice layout or put them on for example a Scrollview and just resize that.

Either way, you'd need to add an observer to rotate it yourself,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

And in didRotate you do the resize, with a nice animated transition, if you like.

- (void) didRotate:(NSNotification *)notification {
 int ori=1;
    UIDeviceOrientation currOri = [[UIDevice currentDevice] orientation];
    if ((currOri == UIDeviceOrientationLandscapeLeft) || (currOri == UIDeviceOrientationLandscapeRight)) ori=0;
}

Hope that sorts you. If Navigation bars or status bars cause the View to get tucked in under the top bar, there are ways to fix that.


There is another work around that I've thought about...

You could use the:

bgView.transform = CGAffineTransformMakeRotate(angle);

You should calculate the angle according to the orientation (it could be M_PI/2, M_PI or 3*M_PI/2)...

You can use this function inside - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration and also wrap it with an animation that will animate for the exact duration as the screen orientation animation:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    // Decide what will be the angle...

    // Make the animation
    [UIView beginAnimations:@"orientationChange" context:nil];
    [UIView setAnimationDuration: duration];
    bgView.transform = CGAffineTransformMakeRotate(angle);
    [UIView commitAnimations];
}

Notice that view's size might be changed because of the top bar...

Notice also that I've never tried it myself...


There is a 'proper' way: Allow the framework to rotate the interface (other wise you'll run in to problems when popping up another view controller) but override the animation of your view.

I have a fullscreen view that contains everything I don't want to rotate, called _fixedView. In storyboard I made sure all autosizing (those red arrows at the 'Size Inspector') are turned off, so the view will always be in the center of the screen.

Then in my view controller I have this override:

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [UIView animateWithDuration:duration
                     animations:^{
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
            _fixedView.transform = CGAffineTransformMakeRotation(M_PI_2);
        } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            _fixedView.transform = CGAffineTransformMakeRotation(-M_PI_2);
        } else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            _fixedView.transform = CGAffineTransformMakeRotation(M_PI);
        } else {
            _fixedView.transform = CGAffineTransformIdentity;
        }}];
}

This way everything in _fixedView is completely fixed (how is that for descriptive names, eh?) While the status bar and my toolbar rotate normally.


I don't know about a 'proper' way, but a work-around is to create the background vie image in both orientations and just change the background view. Good question though - will be interested to see what people come up with as a solution.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜