ios – 强制在全屏MPMoviePlayerController上的横向方向防止在退出全屏时正确旋转

前端之家收集整理的这篇文章主要介绍了ios – 强制在全屏MPMoviePlayerController上的横向方向防止在退出全屏时正确旋转前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个支持所有界面方向的iPhone应用程序(iOS6).但是,当MPMoviePlayerController播放视频全屏时,应仅支持横向.

我在Stack Overflow上找到了以下解决方案,它的工作原理.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.landscapeOnlyOrientation) {
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskAll;
}

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
    self.landscapeOnlyOrientation = YES;
}

- (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
    self.landscapeOnlyOrientation = NO;
}

然而,一个令人讨厌的问题仍然存在,即如果视频以纵向方向退出全屏幕(在强制风景中播放之后),底层视图不会回转.我必须手动将设备旋转到横向并返回到纵向以触发方向的更新.有没有办法我可以以编程方式触发这种更新?

以下屏幕截图应该说明我的意思:

注意:由于各种原因,使用MPMoviePlayerViewController是不可能的.

解决方法

嗨所有我有同样的问题我解决了 –

这是我的完整代码….

你需要先改变appdelegate:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([[[NowPlaying sharedManager] playerViewController] allowRotation])//Place your condition here
{
    return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}

注册全屏控制通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:)
                                             name:MPMoviePlayerWillEnterFullscreenNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:)
                                             name:MPMoviePlayerWillExitFullscreenNotification
                                           object:nil];

然后在播放器控制器中添加一行代码

- (void)moviePlayerWillEnterFullscreenNotification:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(),^
               {
                   self.allowRotation = YES;
               });
}



- (void)moviePlayerWillExitFullscreenNotification:(NSNotification *)notification
{
self.allowRotation = NO;
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];

dispatch_async(dispatch_get_main_queue(),^
               {

                   //Managing GUI in pause condition
                       if (self.currentContent.contentType == TypeVideo && self.moviePlayerController.playbackState == MPMoviePlaybackStatePaused)
                   {
                       [self.moviePlayerController pause];
                       if (self.playButton.selected)
                           self.playButton.selected = NO;
                   }
                   self.view.transform = CGAffineTransformMakeRotation(0);
                   [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
                   self.view.bounds = CGRectMake(0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height);
               });
}

这个代码在iOS6和iOS7中测试工作正常.谢谢 :)

请让我知道如果有任何问题…..

原文链接:https://www.f2er.com/iOS/336486.html

猜你在找的iOS相关文章