2013-05-02 71 views
8

我只想在iOS應用程序的所有視圖控制器中支持垂直方向。但是,我在其中一個視圖控制器中嵌入了一個YouTube視頻,當選擇該視頻佔用全屏幕時,我希望用戶能夠水平定位他/她的手機,以便視頻擴展爲全屏。適用於YouTube嵌入式視頻的iOS 6.0+自轉

編輯:我嘗試使用以下代碼從Autorotate in iOS 6 has strange behaviour

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 

return self.fullScreenVideoIsPlaying ? 
    UIInterfaceOrientationMaskAllButUpsideDown : 
    UIInterfaceOrientationMaskPortrait; 

}

並且在呈現UIWebView/YouTube的幀我的視圖控制器,我已經在這個代碼我viewDidLoad

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(windowNowVisible:) 
    name:UIWindowDidBecomeVisibleNotification 
    object:self.view.window 
]; 

- (void)windowNowVisible:(NSNotification *)notification 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
    appDelegate.fullScreenVideoIsPlaying = !(appDelegate.fullScreenVideoIsPlaying); 
} 

但是,當用戶按下全屏YouTube視頻完成後,如果他/她仍然有電話rizontally,那麼呈現視圖控制器也保持水平(我希望當前的視圖控制器是肖像)。這是一個fullSreenVideoIsPlaying變量的比賽,它的更新速度不夠快,所以我的演示視圖控制器是縱向的。

任何有關如何實現這一目標的反饋將不勝感激。

謝謝!

+0

你可以閱讀 - http://buddingdevelopers.com/autorotation-in-ios/自轉清楚地說明如下。 – Xcoder 2013-05-03 05:56:33

+0

可能的重複:http://stackoverflow.com/questions/13580753/mpmovieplayercontroller-rotating-in-full-screen-while-the-parent-view-controller – 2013-05-09 04:46:59

回答

21

通過將我在StackOverflow上找到的幾個解決方案一起模製成一個解決方案。

不使用該通知

[[NSNotificationCenter defaultCenter] 
     addObserver:self 
     selector:@selector(windowNowVisible:) 
     name:UIWindowDidBecomeVisibleNotification 
     object:self.view.window 
]; 

我用下面的通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil]; 

與實現

-(void) youTubeStarted:(NSNotification*) notif { 
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
    appDelegate.fullScreenVideoIsPlaying = YES; 
} 
-(void) youTubeFinished:(NSNotification*) notif { 
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 
    appDelegate.fullScreenVideoIsPlaying = NO; 
} 

,在我的AppDelegate,我有

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 
    NSUInteger orientations = UIInterfaceOrientationMaskPortrait; 
    if (self.fullScreenVideoIsPlaying) { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
    else {   
     if(self.window.rootViewController){ 
      UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; 
      orientations = [presentedViewController supportedInterfaceOrientations]; 
    } 
return orientations; 
} 

,並在我的視圖控制器,我有

-(BOOL) shouldAutorotate { 
    return NO; 
} 
-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ 
    return UIInterfaceOrientationPortrait; 
} 
+2

只需保留記住使用私人通知('UIMoviePlayerControllerDidEnterFullscreenNotification'和'UIMoviePlayerControllerWillExitFullscreenNotification')可能在未來的iOS版本中崩潰或導致App Store拒絕。 – jszumski 2013-05-06 01:39:10

+0

好的謝謝!你知道我可以使用的任何其他通知嗎? – 2013-05-06 08:11:52

+0

不幸的是,SDK不提供任何iOS 6以上版本。 – jszumski 2013-05-06 11:54:06

6

我以前遇到過同樣的問題,而且我發現在iOS 5.x和6.x下工作的最佳解決方案是以模態視圖呈現視頻控制器。

模態視圖是一個UINavigationController,它包裝視頻控制器並在supportedInterfaceOrientations中響應UIInterfaceOrientationMaskAll。在模態視圖的viewWillAppear:中,我將fullScreenVideoIsPlaying轉換爲YES並將其設置爲NOviewWillDisappear:。這種安排將使底層視圖控制器保持縱向,同時允許模態視圖在用戶認爲合適時旋轉。

+1

感謝您的回覆..有一個快速後續問題。目前,我的YouTube視頻的UIWebView是一個小型單元格(200x100),位於呈現視圖控制器的中間。當它被選中播放時,我該如何在模態視圖控制器中呈現視頻?換句話說,如何獲得用戶在UIWebView中按下播放按鈕的回調? – 2013-05-05 00:44:09

+0

來自Web視圖的視頻播放器將尊重其父設備的旋轉設置,因此您必須將「呈現視圖控制器」設置爲模式視圖。 – jszumski 2013-05-05 02:01:43

+0

據我所知,視頻播放器將尊重其父母的旋轉設置;不過,我的問題是我不希望當前的視圖控制器能夠水平旋轉。只有它呈現的視頻播放器應該能夠水平旋轉 – 2013-05-05 03:08:57