ios 全局禁止横屏 单个视屏全局播放 横屏显示
来源:互联网 发布:java中多线程的使用 编辑:程序博客网 时间:2024/05/29 13:24
最近现在项目开发的 需求是在H5页面嵌套播放视屏的时候 需要全屏显示 横屏模式 但是项目设置的是全局禁止横屏
H5 页面的播放全屏 决解方法
方法总会有的,我们要向监听UIWebView视频播放时候是否全屏,也就是我们要能拿到播放视频的view或者是viewcontroller,但是由于UIWebView没有比较直观的方法,所以只能从其他地方下手了
通过Xcode我们可以查看到view的一些层级关系,可以看出弹出播放的是AVPlayerView,在UIWindow上,
好了,问题到这已经很明晰了,我们要么能拿到AVPlayerView,要呢拿到UIWindow才能控制播放界面,分析后发现AVPlayerView不好拿,但是UIWindow及很easy了。
所以找到解决问题的根据了 那么就是想个点子 将他实现了
1 在 AppDelegate.h 中设置全局 属性 isFull
@property (nonatomic,assign) BOOL isFull; //是否全屏
2 在 AppDelegate.m 文件实现这个方法
// 解决播放不能横屏的问题- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window{ WS(weakSelf); if (weakSelf.isFull) { return UIInterfaceOrientationMaskAll ; }else{ return UIInterfaceOrientationMaskPortrait; }}
3 关键的一 步来了 在需要的实现全屏播放的页面 添加下面的所有方法
#pragma mark - 解决视频不能横屏播放 实现方法// 添加 横屏通知-(void)AddFullScreenNotificationCenter{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全拼播放 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//点击完成}// 移除 横屏通知- (void)RemoveFullScreenNotificationCenter{ [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];}// 进入全拼播放-(void)begainFullScreen{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.isFull = YES;}// 点击完成-(void)endFullScreen{ AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.isFull = 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]; //这里需要设置下 状态栏为显示状态 [[UIApplication sharedApplication] setStatusBarHidden:NO]; }}
通知的添加和移除 在这两个方法中实现
添加通知
移除通知
注意
1 移除通知请在
-(void)viewDidDisappear:(BOOL)animated
方法中调用 因为如果项目没有禁止右滑返回手势的话 其它的方法中实现 会出现提早注销移除
2 在这个方法中需要注意 加上状态栏显示代码
我第一次没加上的时候 播放视频 返回后 状态栏 不见了 ��
非H5 界面 显示全屏设置 没有具体实施过 笑尿了 ��
这个比较容易我的思路是
在APPDelegate.h文件中增加属性:是否支持横屏
/*** 是否允许横屏的标记 */@property (nonatomic,assign)BOOL allowRotation;
在APPDelegate.m文件中增加方法,控制全部不支持横屏
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (self.allowRotation) { return UIInterfaceOrientationMaskAll; } return UIInterfaceOrientationMaskPortrait;}
这样在其他界面想要横屏的时候,我们只要控制allowRotation这个属性就可以控制其他界面进行横屏了。
//需在上面#import "AppDelegate.h"AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];appDelegate.allowRotation = YES;//不让横屏的时候 appDelegate.allowRotation = NO;即可
也没有实践过 通过判断状态栏来设置视图的transform属性 来设置 横屏
- (void)deviceOrientationDidChange: (NSNotification *)notification{ UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue]; CGAffineTransform rotation; switch (interfaceOrientation) { case UIInterfaceOrientationLandscapeLeft: rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0); break; case UIInterfaceOrientationLandscapeRight: rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0); break; case UIInterfaceOrientationPortraitUpsideDown: rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0); break; default: rotation = CGAffineTransformMakeRotation(-startRotation + 0.0); break; } view.transform = rotation;}
- ios 全局禁止横屏 单个视屏全局播放 横屏显示
- XCODE全局禁止横屏
- iOS 全局禁止横屏,但UIWebView 全屏横屏播放视频的解决办法
- 实现iOS全局禁止横屏,但kxmovie全屏横屏播放视频
- iOS 全局禁止横屏,但视频播放界面选择性横屏的解决办法
- iOS 全局禁止横屏,但UIWebView 全屏播放视频,横屏,解决办法
- 全局禁止横屏,但视频播放界面选择性横屏,以及引发问题的解决办法
- iOS 全局竖屏 单个viewcontroller点击按钮支持横屏
- Android学习笔记——Android中全局设置禁止横屏
- IOS 禁止界面横屏显示
- 禁止横屏显示
- ios 禁止横屏
- iOS 禁止横屏
- ios 禁止横屏
- ios 禁止横屏
- ios 禁止横屏
- ios 禁止横屏
- iOS 禁止横屏
- Java程序员面试题集(71-85)
- Python开发爬虫爬取百度百科词条信息(源码下载)
- 《商业的本质》—商业是探求真实、建立互信的过程
- MFC获得当前时间
- Redis入门之浅谈rdb持久化机制
- ios 全局禁止横屏 单个视屏全局播放 横屏显示
- android中状态机原理之状态机的两种写法
- RabbitMQ 开始
- Linux线程之pthread_join
- 安卓开发——判断本机是否安装有某应用(qq、微信、百度地图等)
- MFC中的状态栏
- liux生产环境下的debug调试
- 支持向量机通俗导论(理解SVM的三层境界)
- [Access-Control-Allow-Origin]Web中使用filter实现跨域访问问题