【问题标题】:Allow UIInterfaceOrientationLandscape during UIWebView video playback在 UIWebView 视频播放期间允许 UIInterfaceOrientationLandscape
【发布时间】:2013-06-15 10:03:39
【问题描述】:

我的 iPhone 应用程序是一个仅限纵向的应用程序,在我的应用程序中,我有一个 UITableView,在第一个 UITableCell 中有一个 UIWebViewUIWebView 显示嵌入的 youtube 视频。当我点击视频播放时,它进入全屏模式。我需要做的是,允许用户旋转他们的设备并以横向模式播放视频。然后当视频停止时,只允许再次纵向。我设置在视频进入全屏并离开全屏时收听通知。但我不知道如何以编程方式允许用户旋转界面方向。

所以基本上我在通知传递时调用了 2 个方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youTubeFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];

-(void)youTubeStarted:(NSNotification *)notification{
    // Entered fullscreen code goes here.
}

-(void)youTubeFinished:(NSNotification *)notification{
    // Left fullscreen code goes here.
}

我会在这 2 种方法中添加什么以仅在视频播放期间允许方向更改?

【问题讨论】:

标签: iphone objective-c uiwebview


【解决方案1】:

如果您只想支持全屏横向播放。从 iOS 8 开始,来自UIWebView 的视频在AVPlayerViewController 内播放。

在您的 AppDelegate 中检查呈现窗口是否包含 AVPlayerViewController

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if let w = window, let root = w.rootViewController {

        if root.childViewControllers.first is AVPlayerViewController {  
            return UIInterfaceOrientationMask.allButUpsideDown
        }
    }

    return UIInterfaceOrientationMask.portrait
}

【讨论】:

    【解决方案2】:

    我想通了。在我的两种方法中:

    -(void)youTubeStarted:(NSNotification *)notification{
       // Entered Fullscreen code goes here..
       AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
       appDelegate.fullScreenVideoIsPlaying = YES;
    }
    
    -(void)youTubeFinished:(NSNotification *)notification{
       // Left fullscreen code goes here...
       AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
       appDelegate.fullScreenVideoIsPlaying = NO;
    
       //CODE BELOW FORCES APP BACK TO PORTRAIT ORIENTATION ONCE YOU LEAVE VIDEO.
       [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
        //present/dismiss viewcontroller in order to activate rotating.
        UIViewController *mVC = [[UIViewController alloc] init];
        [self presentModalViewController:mVC animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
    

    我通过设置 AppDelegate 的 BOOL 属性在上述方法中访问了我的 App 委托。然后我在我的 AppDelegate.m 中调用了下面的应用方法:

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

    self.fullScreenVideoIsPlaying 是我在 AppDelegate.h 文件中设置为属性的 BOOL

    我希望这可以帮助其他人解决我失去的 5 个小时。

    【讨论】:

    • 谢谢!!我花了几个小时寻找这个问题的答案,而你的解决方案是迄今为止最简单的。非常感谢:)
    • 我一直在寻找几个小时来解决我的轮换问题。这解决了它;但是,我似乎无法让它在视频结束后禁用旋转。你把youTubeFinished的通知调用放在哪里了?
    • 没关系,我修好了。我意识到我的整个应用程序都在旋转。为了解决这个问题,我刚刚删除了 supportedInterfaceOrientationsForWindow 方法中的 if(self.window.rootViewController) 语句。现在一切正常。感谢您发布此信息!
    • 嗨@brownieface 删除后我也无法停止我的应用程序中的方向。并且在完成视频应用程序后仍处于模式景观中。它不会强行改变。请帮助我做错了什么
    • 如何触发这两种方法?触摸时向 WebView 添加目标?
    【解决方案3】:

    您将拥有一个全局状态变量,可能在您的应用程序委托中或您存储应该可全局访问的变量的任何位置,然后将其设置为truefalse,具体取决于电影播放器​​是全屏还是全屏不是。在被调用以旋转应用程序的回调中,您检查变量的状态,并根据该返回值是否允许旋转。

    【讨论】:

    • 我可能应该提到,出于这个确切原因,我也设置了一个 BOOL,但我未能尝试更改回调中是否允许旋转以旋转应用程序。你能告诉我一些允许这样做的代码吗?
    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 2011-06-17
    • 2012-10-27
    相关资源
    最近更新 更多