2016-09-23 5 views
0

私は両方のipadのiphone、ipadのユーザーのためのアプリケーションを持っています常にランドスケープモードが有効になり、iphoneユーザーのための肖像画、今私が達成しようとしているものは、私はAVPlayerViewControllerアプリケーションはappdelegate内の肖像画でロックされています。プレーヤーのフルスクリーンボタンを押すと、それはちょうどopnポートレートモードに留まります。私はそれを風景にしたいです。私はstackoverflowで見つけたすべての答えを試しましたが、それは動作しますか?AVPlayerviewcontroller ios objective c

+0

私の答えはあなたのために働いていますか? –

+0

いいえ、それはありませんでした。とにかく、ありがとう。 –

答えて

0

私は良い習慣を知らないが、これは毎秒呼び出されるメソッドを作っNSTimerでこの問題を解決し私がしたこと:

[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(yourMethod) userInfo:nil repeats:YES]; 

-(void)yourMethod{ 
if(!_fullscreen){ 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 
}else{ 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 
} } 

_fullscreenは、AVplayerがフルスクリーン表示または通常表示に切り替わるときに変更されるフラグです。

AVplayerの状態を追跡するために、Observer @ "bounds"を使用して、AVPlayerの画面のサイズを確認しました。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context { 
if (object == self.playerViewController.contentOverlayView) { 
    if ([keyPath isEqualToString:@"bounds"]) { 
     CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue], newBounds = [change[NSKeyValueChangeNewKey] CGRectValue]; 
     BOOL wasFullscreen = CGRectEqualToRect(oldBounds, [UIScreen mainScreen].bounds), isFullscreen = CGRectEqualToRect(newBounds, [UIScreen mainScreen].bounds); 
     if (isFullscreen && !wasFullscreen) { 
      if (CGRectEqualToRect(oldBounds, CGRectMake(0, 0, newBounds.size.height, newBounds.size.width))) { 
       NSLog(@"rotated fullscreen"); 
      } 
      else { 
       NSLog(@"entered fullscreen"); 

       if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 

       }else{ 
        _fullscreen = YES; 
        [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 
       } 
      } 
     } 
     else if (!isFullscreen && wasFullscreen) { 
      NSLog(@"exited fullscreen"); 
      if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 

      }else{ 
       _fullscreen = NO; 
       [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 

      } 
     } 
    } 
}} 
0

AppDelegate.mファイルに下記のコードを追加してください。

のObjective C

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0 
#define supportedInterfaceOrientationsReturnType NSUInteger 
#else 
#define supportedInterfaceOrientationsReturnType UIInterfaceOrientationMask 
#endif 

- (supportedInterfaceOrientationsReturnType)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx 
{ 
    if ([[self.window.rootViewController presentedViewController] isKindOfClass:[AVPlayerViewController class]] 
     ) 
    { 
     if ([self.window.rootViewController presentedViewController].isBeingDismissed) 
     { 
      return UIInterfaceOrientationMaskPortrait; 
     }else{ 
      return UIInterfaceOrientationMaskAll; 
     } 
    } 
} 

スウィフト

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0 
let supportedInterfaceOrientationsReturnType = NSUInteger 
#else 
let supportedInterfaceOrientationsReturnType = .mask 
#endif 

func application(_ application: UIApplication, supportedInterfaceOrientationsFor windowx: UIWindow) -> supportedInterfaceOrientationsReturnType { 
    if (self.window!.rootViewController!.presented! is AVPlayerViewController) { 
     if self.window!.rootViewController!.presented!.isBeingDismissed() { 
      return .portrait 
     } 
     else { 
      return .all 
     } 
    } 
}