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


禁止轉屏是這個意思,在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];
    }
}

這樣就完成了

 


免責聲明!

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



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