适用于youtube嵌入式视频的iOS 6.0+自动旋转 [英] iOS 6.0+ autorotation for youtube embedded video

查看:249
本文介绍了适用于youtube嵌入式视频的iOS 6.0+自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to only support vertical orientation throughout all the view controllers of my iOS app. 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 horizontally so the video expands to take the full screen.

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

I tried using the following code from Autorotate in iOS 6 has strange behaviour:

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

return self.fullScreenVideoIsPlaying ?
    UIInterfaceOrientationMaskAllButUpsideDown :
    UIInterfaceOrientationMaskPortrait;

}

和我的视图控制器显示 UIWebView / YouTube框架,我在 viewDidLoad 中有此代码:

and in my view controller that presents the UIWebView/YouTube frame, I have this code in my 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 变量的竞赛,该变量的更新速度不够快,因此我的呈现视图控制器是纵向的。

However, when the user presses done on the fullscreen YouTube video, if he/she still has the phone horizontally, then the presenting view controller also stays horizontal (I want the present view controller to be portrait). It's a race on the fullSreenVideoIsPlaying variable which isn't updating fast enough so that my presenting view controller is portrait.

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

Any feedback on how to achieve this would be greatly appreciated.

谢谢!

推荐答案

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

Figured out a solution by molding together a few solutions I've found on 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中,我有

and in my AppDelegate, I have

 - (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;
 }

在我的视图控制器中,我有

and in my view controllers, I have

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

这篇关于适用于youtube嵌入式视频的iOS 6.0+自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆