iOS 全局禁止橫屏,但UIWebView 全屏播放視頻,橫屏,解決辦法(任意頁面橫豎屏或禁止)


iOS 全局禁止橫屏,但UIWebView 全屏播放視頻,橫屏,解決辦法

iOS 全局禁止橫屏,但UIWebView 全屏播放視頻,橫屏,解決辦法

UIWebview在播放網頁視頻的時候我們需要進行是否全屏狀態的監聽。

一般的需求是在播放視頻時候需要橫屏,退出全屏的時候不能橫屏,但是UIWebview沒有給出響應的方法,

本問題解決的Demo工程https://github.com/darren90/iOS_Demo/tree/master/02-UIWebview

1:其他界面不支持橫屏:

這個比較容易我的思路是在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;即可

2:播放界面橫屏:

2.1:播放界面橫屏:網上的解決方案

網上有人給出兩種方法:

第一:但是iOS8下失效,沒有用[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(videoStarted:)name:@"UIMoviePlayerControllerDidEnterFullscreenNotification"object:nil];//

第二:通過js,但是沒有找到詳細的,可解決的方案。

所以也就是沒有找到成熟的方案,所以就自己分析了。

2.2:播放界面橫屏:問題分析

方法總會有的,我們要向監聽UIWebView視頻播放時候是否全屏,也就是我們要能拿到播放視頻的view或者是viewcontroller,但是由於UIWebView沒有比較直觀的方法,所以只能從其他地方下手了

通過Reveal我們可以查看到view的一些層級關系,可以看出彈出播放的是AVPlayerView,在UIWindow上,

好了,問題到這已經很明晰了,我們要么能拿到AVPlayerView,要呢拿到UIWindow才能控制播放界面,分析后發現AVPlayerView不好拿,但是UIWindow及很easy了。

2.3:播放界面橫屏:問題解決

所以這里可以使用UIWindow的通知,就可以解決

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//進入全屏 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏

在退出全屏時,增加邏輯讓其強制編程豎屏,這樣當全屏播放的時候,點擊down("完成")時,就會自動變成豎屏了。

// 進入全屏 -(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];  } }

實例Demo工程 https://github.com/darren90/iOS_Demo/tree/master/02-UIWebview

歡迎您的訪問...

微博:@IT_攻城師

github: @Darren90

作者: http://www.cnblogs.com/fengtengfei/

本文版權歸本人和博客園所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM