8

我只想在我的 iOS 应用程序的所有视图控制器中支持垂直方向。However, I embed a YouTube video in one of my view controllers, and when that video is selected to take up the full screen, I want the user to be able to orient his/her phone horizo​​ntally so the video expands to take the full screen .

编辑:我尝试在 iOS 6 中使用 Autorotate 中的以下代码有奇怪的行为

- (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更新速度不够快,因此我的呈现视图控制器是纵向的。

任何有关如何实现这一目标的反馈将不胜感激。

谢谢!

4

2 回答 2

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;
 }
于 2013-05-06T00:59:32.040 回答
6

我之前也遇到过同样的问题,我能找到的在 iOS 5.x 和 6.x 下工作的最佳解决方案是在模态视图中显示视频控制器。

模态视图是一个包装视频控制器并以inUINavigationController响应的视图。在模态视图中,我翻转到并将其设置为in 。这种安排将使底层视图控制器保持纵向,同时允许模态视图根据用户认为合适的方式旋转。UIInterfaceOrientationMaskAllsupportedInterfaceOrientationsviewWillAppear:fullScreenVideoIsPlayingYESNOviewWillDisappear:

于 2013-05-05T00:32:09.563 回答