2013-05-02 49 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