iOS Plus 特定页面横屏退出或切换其他页面再进入仍保持横屏

http://www.jianshu.com/p/f917df3e3b2e

最近在研究播放器,播放器需要根据手机旋转来切换横竖屏状态,且横竖屏都是同一个VC只是做了屏幕尺寸适配,全屏播放页面有一个分享按钮,参考其他APP要实现,分享到微博微信等强制竖屏APP之后回来仍旧保持横屏,而不是竖屏再切换回横屏。如下图所示。


IMG_0510.PNG
APP所有VC横屏或竖屏

AppDelegate.m中有如下方法可以设置整个app的方向。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
      return UIInterfaceOrientationMaskLandscape;
}

该方法实质上是制定window的方向,虽然一个app有多个VC,但是window都只有一个,所以这个指定了window的方向,那所有VC的方向就都是统一的。

特定VC横屏或竖屏

在iOS6之后,需要在某个页面强制横屏,可以使用以下代码:

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationMaskLandscape);
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

此段代码针对于在特定VC指定横竖屏方向,但是由于我的demo中横竖屏都是同一个VC,这个方法就不能采用了。

特定状态横屏且window横屏

如果需要app进入其他app再回来或退出后重新进入都保持原来的方向,那么就需要改变window的方向,就需要用到APPDelegate中的supportedInterfaceOrientationsForWindow方法。只在特定状态横屏,那就需要一个标识符来判定当前是否需要横屏。代码如下:

//AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, assign) BOOL isLandscape;
@end

//AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (_ isLandscape) {
            return UIInterfaceOrientationMaskLandscape;
    }
    else
    {
           return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

//Player.m
- (void)backToPortrait//回到竖屏
{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = NO;
}

- (void)enterToLandscape//进入横屏
{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = YES;
}

但是发现加入以上代码,导致全屏模式下无法通过旋转手机方向进入回到竖屏模式了,看了一下是因为isLandscape为YES,所以始终return UIInterfaceOrientationMaskLandscape。最终修改AppDelegate.m中代码如下:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (_isLandscape) {
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
            return UIInterfaceOrientationMaskLandscape;
        }else { // 横屏后旋转屏幕变为竖屏
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    else
    {
           return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

此处需要注意,判断屏幕方向只能使用UIDeviceOrientation手机设备方向而不能用UIInterfaceOrientation状态栏方向,因为横屏状态下,走isLandscape逻辑,不改变当前VC方向,所以虽然手机方向改变,但状态栏方向不变,用状态方向判断会导致屏幕无法旋转。



文/Fiona_L(简书作者)
原文链接:http://www.jianshu.com/p/f917df3e3b2e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值