用于youtube嵌入式视频的iOS 6.0+自动旋转

时间:2013-05-02 20:53:05

标签: ios objective-c ios6 uiviewcontroller auto-rotation

我想只在我的iOS应用的所有视图控制器中支持垂直方向。但是,我在我的一个视图控制器中嵌入了一个YouTube视频,当选择该视频占据整个屏幕时,我希望用户能够水平定位他/她的手机,以便视频扩展为全屏

编辑:我尝试使用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视频上按下完成时,如果他/她仍然水平地拥有电话,则呈现视图控制器也保持水平(我希望当前视图控制器是纵向)。这是fullSreenVideoIsPlaying变量的竞赛,它的更新速度不够快,因此我的视图控制器是纵向的。

非常感谢任何有关如何实现这一目标的反馈。

谢谢!

2 个答案:

答案 0 :(得分: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;
 }

答案 1 :(得分:6)

之前我遇到过同样的问题,我能找到的最好的解决方案是在iOS 5.x和6.x下运行的,它是以模态视图呈现视频控制器。

模态视图是一个UINavigationController,用于封装视频控制器,并在UIInterfaceOrientationMaskAll中以supportedInterfaceOrientations进行响应。在模态视图的viewWillAppear:中,我将fullScreenVideoIsPlaying翻转为YES并将其设置为NO中的viewWillDisappear:。这种安排将使底层视图控制器保持纵向,同时允许模态视图按用户认为合适的方式旋转。