前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 全局禁止横屏,但视频播放界面选择性横屏的解决办法

iOS 全局禁止横屏,但视频播放界面选择性横屏的解决办法

作者头像
Originalee
发布2018-08-30 10:39:50
3.8K0
发布2018-08-30 10:39:50
举报
文章被收录于专栏:编程之旅

有时我们的APP并没有适配横屏的需求,但是在个别视频播放界面,我们需要在播放视频的时候横屏,退出全屏的时候不能横屏,但是有时候并没有原生API并没有给出解决方案。

当其他界面不支持横屏时:

这个解决方法比较容易

APPDelegate.h 文件中增加属性:是否支持横屏

代码语言:javascript
复制
/***  是否允许横屏的标记 */
@property (nonatomic,assign)BOOL allowRotation;

APPDelegate.m 文件中增加方法,控制全部不支持横屏

代码语言:javascript
复制
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {
        return  UIInterfaceOrientationMaskAllButUpsideDown;
    }
    return UIInterfaceOrientationMaskPortrait;
}

这样在其他界面想要横屏的时候,我们只要控制 allowRotation 这个属性就可以控制其他界面进行横屏了。

代码语言:javascript
复制
//需在上面#import "AppDelegate.h"
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
//不让横屏的时候 appDelegate.allowRotation = NO;即可

播放界面横屏

所以这里可以使用 UIWindow 的通知,就可以解决

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏

在退出全屏时,增加逻辑让其强制编程竖屏,这样当全屏播放的时候,点击 down("完成") 时,就会自动变成竖屏了。

代码语言:javascript
复制
// 进入全屏
-(void)begainFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allowRotation = NO;
    
    //强制归正:
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val =UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

上面的两个方案已经足够解决只有部分界面需要支持横屏的问题了,亲测可用

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016.07.14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 当其他界面不支持横屏时:
  • 播放界面横屏
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档