iOS 11 實現App在禁止轉屏的狀態下網頁播放器全屏

來源:互聯網
上載者:User

標籤:scl   uid   技術分享   auto   err   實現   mst   ring   int   

禁止轉屏是這個意思,在General中設定Device Orientation只有豎屏。

要點就是重寫UIViewController的以下3個屬性方法

系統的全屏視頻播放器是AVFullScreenViewController,但並未暴露出任何的API,所以要在UIViewController的擴充中去修改

以下是UIViewController擴充中的實現方法,判斷只有視頻播放器才轉屏

- (BOOL)shouldAutorotate {    if ([self isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {        return YES;    }        return NO;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations {    if ([self isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {        return UIInterfaceOrientationMaskLandscapeRight;    }    return UIInterfaceOrientationMaskPortrait;}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {    if ([self isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {        return UIInterfaceOrientationLandscapeRight;    }    return UIInterfaceOrientationPortrait;}

 

還沒有結束,需要在AppDelegate中重寫

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window  NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

這個系統方法。其實只需要支援豎屏和播放器橫屏兩種狀態就夠了,但是那樣需要加判斷,在轉屏的時候返回不同值。所以這裡直接用UIInterfaceOrientationMaskAll更為簡潔。

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

接下來就要在需要開啟全屏播放的控制器中實現邏輯了。

添加BOOL變數判斷是否載入完

@property (nonatomic,assign)BOOL didWebViewLoadOK;

初始化時加入下面兩個通知

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

別忘記移除

- (void)dealloc {    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];}

WebView的代理

- (void)webViewDidFinishLoad:(UIWebView *)webView {    self.didWebViewLoadOK = YES;}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {    self.didWebViewLoadOK = NO;}

實現通知

#pragma mark - Notification-(void)begainFullScreen{    if(!self.didWebViewLoadOK) {        return;    }        [[UIDevice currentDevice] setValue:@"UIInterfaceOrientationLandscapeLeft" forKey:@"orientation"];    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 = UIInterfaceOrientationLandscapeLeft;        [invocation setArgument:&val atIndex:2];        [invocation invoke];    }}- (void)endFullScreen{    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];    }}

這樣就完成了

 

iOS 11 實現App在禁止轉屏的狀態下網頁播放器全屏

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.