2014-10-31 62 views
0

我見過類似的問題已經問過,所以我錯過了正確的答案。 我的應用程序必須始終處於肖像模式,除非它顯示媒體播放器必須處於橫向全屏模式。如何在橫向模式下顯示MPVideoPlayer和縱向顯示所有其他視圖?

下面是它如何做的:

AppDelegate.m

@implementation HCAAppDelegate 

+(void) landscapeLock { 
    HCAAppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
    appDelegate.screenIsLandscapeOnly= true; 
    appDelegate.screenIsPortraitOnly = false; 
} 

+(void) portraitLock 
{ 
    HCAAppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
    appDelegate.screenIsPortraitOnly = true; 
    appDelegate.screenIsLandscapeOnly = false; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
... 
    [HCAAppDelegate portraitLock]; 
} 


- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 
    NSUInteger orientations = UIInterfaceOrientationMaskPortrait; 
    if (self.screenIsPortraitOnly) { 
     return UIInterfaceOrientationMaskPortrait; 
    } else if (self.screenIsLandscapeOnly) { 
     return UIInterfaceOrientationMaskLandscape; 
    } else { 
     if(self.window.rootViewController) { 
      UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject]; 
      orientations = [presentedViewController supportedInterfaceOrientations]; 
     } 
     return orientations; 
    } 
} 

媒體播放器創建爲

-(void) _initPlayer 
{ 
    [HCAAppDelegate landscapeLock]; 

    _moviePlayer = [[HCAMoviePlayerController alloc]init]; 
    [_moviePlayer setControlStyle:MPMovieControlStyleDefault]; 

    _moviePlayer.shouldAutoplay = YES; 

    [self.view addSubview:_moviePlayer.view]; 
    [_moviePlayer setFullscreen:YES animated:YES]; 
} 

,並斥爲

- (void) _finishPlay 
{ 
... 
    [_moviePlayer.view removeFromSuperview]; 
    [HCAAppDelegate portraitLock]; 
} 

當玩家被初始化它轉到l風景,但是當它被解散時它仍然處於風景模式,爲什麼portraitLock方法不起作用? 謝謝!

回答

0

在深入瞭解這個問題之後,我明白了錯誤在哪裏。將發佈答案在這裏關閉案例,並提供一個工作示例的主題。 將mediaPlayer視圖添加到現有的視圖控制器 [self.view addSubview:_moviePlayer.view]; 作品但刪除後 [_moviePlayer.view removeFromSuperview]; 的portraitLock不影響self.view

的工作方式是創建一個新的控制器和現在/駁回:

這裏是代碼: 呈現全屏播放的看法。注意,高度/寬度被切換在 線_moviePlayer.view SETFRAME:

UIViewController *movieVC = (UIViewController*) [storyboard instantiateViewControllerWithIdentifier:@"MoviePlayer"]; 

movieVC.view = [[UIView alloc] initWithFrame:CGRectMake(0, 
                 0, 
                 [[UIScreen mainScreen] applicationFrame].size.width, 
                 [[UIScreen mainScreen] applicationFrame].size.height)]; 

[HCAAppDelegate landscapeLock]; 

[_moviePlayer.view setFrame:CGRectMake(0, 
             0, 
             movieVC.view.frame.size.height, 
             movieVC.view.frame.size.width)]; 

[movieVC.view addSubview:_moviePlayer.view]; 
[self.navigationController presentViewController:movieVC animated:YES completion:nil]; 

駁回視圖:

[_moviePlayer stop]; 
[HCAAppDelegate portraitLock]; 
[self.navigationController dismissViewControllerAnimated:YES completion:nil]; 
相關問題