自动旋转从UIWebView </video>中的HTML5 <video>播放的视频

时间:2011-03-16 15:11:38

标签: iphone uiviewcontroller html5-video autorotate

我的应用程序出于某些原因,仅支持纵向方向 但是,在某些情况下,我需要显示来自UIWebView(带有video标签)的视频,如果用户可以纵向或横向查看它,那将会很不错。

控制器配置如下:

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

结果:视频仅以纵向模式播放(确定,非常期待)。

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

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

结果:横向模式可用(太棒了!)但是如果用户在横向播放时点击“完成”,则一旦玩家被解雇,之前的视图将以横向模式显示(不是很好) )。

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

1 个答案:

答案 0 :(得分:2)

我做过非常相似的事情。当你弹出控制器时,你必须修改UIView堆栈以强制app调用shouldAutorotateToInterfaceOrientation。

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

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

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

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

创建并分配UINavigationControllerDelegate 在委托中,当控制器弹出或按下时暂时修改UIView堆栈:

- (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];
    }
}

这是一种肮脏的黑客,但它确实有效。