8

iOS アプリのすべてのビュー コントローラーで垂直方向のみをサポートしたいと考えています。ただし、ビューコントローラーの1つにYouTubeビデオを埋め込み、そのビデオが全画面表示に選択されている場合、ユーザーが携帯電話を水平に向けて、ビデオが拡大して全画面表示になるようにしたい.

編集: iOS 6 の Autorotate から次のコードを使用してみましたが、奇妙な動作があります:

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

return self.fullScreenVideoIsPlaying ?
    UIInterfaceOrientationMaskAllButUpsideDown :
    UIInterfaceOrientationMaskPortrait;

}

UIWebView/YouTube フレームを表示するビュー コントローラーでは、次のコードが my.xml に含まれてい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;
 }

そして私のView Controllerでは、私は持っています

 -(BOOL) shouldAutorotate {
     return NO;
 }
 -(NSUInteger)supportedInterfaceOrientations{
     return UIInterfaceOrientationMaskPortrait;
 }
 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
     return UIInterfaceOrientationPortrait;
 }
于 2013-05-06T00:59:32.040 に答える