標籤: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在禁止轉屏的狀態下網頁播放器全屏