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

Roh*_*wal 8 objective-c uiviewcontroller ios auto-rotation ios6

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

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

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

return self.fullScreenVideoIsPlaying ?
    UIInterfaceOrientationMaskAllButUpsideDown :
    UIInterfaceOrientationMaskPortrait;
Run Code Online (Sandbox Code Playgroud)

}

在我的视图控制器中显示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);
}
Run Code Online (Sandbox Code Playgroud)

然而,当用户在全屏YouTube视频上按下完成时,如果他/她仍然水平地具有电话,则呈现视图控制器也保持水平(我希望当前视图控制器是肖像).这是fullSreenVideoIsPlaying变量的竞赛,它没有足够快地更新,因此我的呈现视图控制器是纵向的.

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

谢谢!

Roh*_*wal 21

通过将我在StackOverflow上找到的一些解决方案组合在一起来找出解决方案.

而不是使用此通知

 [[NSNotificationCenter defaultCenter]
      addObserver:self
      selector:@selector(windowNowVisible:)
      name:UIWindowDidBecomeVisibleNotification
      object:self.view.window
 ];
Run Code Online (Sandbox Code Playgroud)

我使用以下通知

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

与实现

 -(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;
 }
Run Code Online (Sandbox Code Playgroud)

在我的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;
 }
Run Code Online (Sandbox Code Playgroud)

在我看来,控制器,我有

 -(BOOL) shouldAutorotate {
     return NO;
 }
 -(NSUInteger)supportedInterfaceOrientations{
     return UIInterfaceOrientationMaskPortrait;
 }
 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
     return UIInterfaceOrientationPortrait;
 }
Run Code Online (Sandbox Code Playgroud)

  • 我最终做的是在委托方法中返回UIInterfaceOrientationMaskAllButUpsideDown,然后继承UITabBarController(我的根视图控制器)以返回-supportedInterfaceOrientations中的UIInterfaceOrientationMaskPortrait.这样,视频可以在横向播放,但是标签栏控制器的所有子视图控制器仅支持纵向. (3认同)
  • 请记住,使用私人通知(`UIMoviePlayerControllerDidEnterFullscreenNotification`和`UIMoviePlayerControllerWillExitFullscreenNotification`)可能会在未来的iOS版本中破坏或导致App Store拒绝. (2认同)

jsz*_*ski 6

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

该模式的看法是UINavigationController,它包装的视频控制器,并响应UIInterfaceOrientationMaskAllsupportedInterfaceOrientations.在模式的看法的viewWillAppear:,我不停地翻转fullScreenVideoIsPlaying,以YES将其设置为NOviewWillDisappear:.这种安排将使底层视图控制器保持纵向,同时允许模态视图在用户认为合适时旋转.