1

我在 iOS 6 上的视图控制器中的用户界面方向遇到了一些问题。基本上,我有一个同时支持纵向和横向模式的视图控制器。这当然可以正常工作,但我还需要实现在按下时将在横向和纵向模式之间切换的按钮 - 无需用户旋转设备。我基本上需要与 youtube 应用程序中的视频播放器相同的行为。

我曾尝试使用:

[UIApplication sharedApplication] setStatusBarOrientation:animated:]

问题是,这仅适用于 iOS 5。在 iOS 6 上,此功能在我实现之前不起作用:

- (NSUInteger)supportedInterfaceOrientations {
    return 0;
}

问题来了……当我在掩码中返回 0 时,视图控制器的视图不会自动旋转内容(当然),唯一的方法是手动旋转它,如下所示:

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

-(void)didRotate:(NSNotification *) notification
{
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    UIInterfaceOrientation interfaceOrientation;
    switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
            interfaceOrientation = UIInterfaceOrientationPortrait;
            break;
        case UIDeviceOrientationLandscapeLeft:
            interfaceOrientation = UIInterfaceOrientationLandscapeRight;
            break;
        case UIDeviceOrientationLandscapeRight:
            interfaceOrientation = UIInterfaceOrientationLandscapeLeft;
            break;
        default:
            interfaceOrientation = UIInterfaceOrientationPortrait;
            break;
    }
    if([UIApplication sharedApplication].statusBarOrientation != interfaceOrientation)
        [self layoutForInterfaceOrientation:interfaceOrientation];
}

...

- (void)layoutForInterfaceOrientation:(UIInterfaceOrientation)orientation
{

    [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:YES];

    CGFloat angle = 0.0f;
    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            angle = 0.0f;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            angle = -M_PI / 2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            angle = M_PI / 2;
            break;
        default:
            break;
    }

    [UIView animateWithDuration:0.3f animations:^(void){

        [[UIApplication sharedApplication] setStatusBarOrientation:orientation  animated:NO];
        [self.view setTransform: CGAffineTransformMakeRotation(angle)];

    }];
...

这个解决方案对我来说似乎真的很脏。有没有更简单的方法可以在 iOS 6 上使用按钮实现横向/纵向模式的切换?

非常感谢!

4

0 回答 0