自动旋转从 HTML5 <video> 播放的视频在 UIWebView [英] Autorotating video played from HTML5 <video> in UIWebView

查看:37
本文介绍了自动旋转从 HTML5 <video> 播放的视频在 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我的应用仅支持纵向.
但是,在某些情况下,我需要从 UIWebView(带有 video 标签)显示视频,如果用户可以纵向或横向查看它会很好.

My app, for some reasons, only supports portrait orientation.
However, in some cases I need to display videos from a UIWebView (with video tag) and it would be nice if the user can view it either in portrait or landscape.

控制器配置如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

结果:视频仅在纵向模式下播放(好的,在意料之中).

Result: the video is played only in portrait mode (ok, quite expected).

我试过了:
- 设置此项以在用户启动视频时支持所有方向
- 当视频停止时,回到仅限肖像"

I tried:
- setting this to support all orientations when the user starts a video
- when the video stops, back to "portrait only"

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    // autorotationEnabled is toggled when a video is played / stopped
    return autorotationEnabled ? YES : UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

结果:横向模式可用(太棒了!)但如果用户在横向播放时点击完成",则一旦玩家被解散,上一个视图将以横向模式显示(不太好)).

Result: landscape mode is available (great!) but if the user taps 'Done' while playing in landscape, the previous view will appear in landscape mode once the player is dismissed (not so great).

有没有人知道如何在玩家被解散时防止控制器以横向模式显示?
(使用 UIDevice 的私有方法 setInterfaceOrientation 不是一种选择)

Does anybody have an idea on how to prevent the controller from being displayed in landscape mode when the player is dismissed?
(using the private method setInterfaceOrientation of UIDevice is not an option)

推荐答案

我做过类似的事情.您必须修改 UIView 堆栈以强制应用在弹出控制器时调用 shouldAutorotateToInterfaceOrientation.

I've have done something very similar. You have to modify the UIView stack to force the app to call shouldAutorotateToInterfaceOrientation when you pop the controller.

在您的 WebViewController 中设置为允许自动旋转:

In your WebViewController set to allow the autorotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}

在父控制器中禁止自动旋转:

In the parent controller disallow autorotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

创建并分配一个 UINavigationControllerDelegate.
在delegate中,在控制器弹出或推送时临时修改UIView堆栈:

Create and assign a UINavigationControllerDelegate.
In the delegate, temporarily modify the UIView stack when the controller pops or pushes:

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

// Force portrait by changing the view stack to force autorotation!
if ([UIDevice currentDevice].orientation != UIInterfaceOrientationPortrait) {
    if (![viewController isKindOfClass:[MovieWebViewController class]]) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *view = [window.subviews objectAtIndex:0];
        [view removeFromSuperview];
        [window insertSubview:view atIndex:0];
    }
}

这是一个肮脏的黑客,但它有效.

It's kind of a dirty hack, but it works.

这篇关于自动旋转从 HTML5 <video> 播放的视频在 UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆