重新加载supportedInterfaceOrientationsForWindow或以横向模式允许MPMoviePlayerViewController

时间:2014-10-17 01:45:44

标签: ios objective-c iphone orientation

我想在我的仅肖像应用中使用MPMoviePlayerViewController播放电影。所以,我喜欢这个。

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

    if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]]) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

因此,我可以在横向模式下玩游戏。但是,当我忽略我的应用程序处于横向模式时。那么,有没有办法在 supportedInterfaceOrientationsForWindow 中动态地将我的应用程序更改为纵向模式。我知道我可以在MPMoviePlayerViewController退出时收到通知。但我不知道如何在退出时将我的应用程序设置回纵向。

1 个答案:

答案 0 :(得分:0)

1)设置设备方向:仅限纵向模式。

2)之后,去你的ViewController.m类(你要在哪里展示MPMoviePlayerViewController),并添加这个通知:

- (void)viewWillAppear:(BOOL)animated{
    UIDevice *device = [UIDevice currentDevice];                    //Get the device object
    [device beginGeneratingDeviceOrientationNotifications];         //Tell it to start monitoring the accelerometer for orientation

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    //Get the notification centre for the app
    [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
}

- (void)orientationChanged:(NSNotification *)note{
    UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
    if (devOrientation==4||devOrientation==3){
        //Now rotate the frame of your MPMoviePlayerViewController for Landscape mode
        [movieController.view setFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
        [movieController.view setTransform:CGAffineTransformMakeRotation(-M_PI_2)];

        CGRect slFr6=movieController.view.frame;
        slFr6.origin.x=0;
        slFr6.origin.y=0;
        movieController.view.frame=slFr6;
    }
    else if (devOrientation==1||devOrientation==2){
        ////Now rotate the frame of your MPMoviePlayerViewController for Portrait mode
        [movieController.view setTransform:CGAffineTransformMakeRotation(0)];
        [movieController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

        CGRect slFr6=movieController.view.frame;
        slFr6.origin.x=0;
        slFr6.origin.y=0;
        movieController.view.frame=slFr6;
    }
}

//For Adding MPMoviePlayerController use it
-(void)createPlayer{
    MPMoviePlayerController *movieController = [[MPMoviePlayerController alloc] init];
    movieController.controlStyle = MPMovieControlStyleDefault;//MPMovieControlStyleNone;
    [movieController setContentURL:_url];
    [movieController.view setFrame:CGRectMake (0,0,self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:movieController.view];
    [movieController prepareToPlay];
    [movieController play];
}