本文介绍了在UIWebView中横向模式的Youtube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序不适用于景观。但是当我在 UIWebView 中打开我的YouTube频道并且用户启动视频时,它会显示在肖像中。如果用户转动他的iPhone,我想让它以横向模式显示。



如何启用横向模式,就像这种情况一样? / p>

我知道有脏黑客这样做,但我更喜欢更干净的东西。此外,我不希望UIWebView切换到横向,但只是视频可以。

解决方案

我终于调整了我的查看以便使用以下代码支持横向模式:

   - (void)viewDidLoad {
[super viewDidLoad] ;

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged :)
name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice] .orientation;
if(UIDeviceOrientationIsLandscape(deviceOrientation))
{
//我为此方向设置了我的新框架原点和大小
isShowingLandscapeView = YES;
}
else if(deviceOrientation == UIDeviceOrientationPortrait)
{
//我为此方向设置了我的新框架原点和大小
isShowingLandscapeView = NO;
}
}

- (无)orientationChanged:(NSNotification *)通知
{
//我们必须在此添加延迟,否则我们' ll在新视图中交换
//太快了,我们将得到一个动画故障
[self performSelector:@selector(updateLandscapeView)withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice] .orientation;
if(UIDeviceOrientationIsLandscape(deviceOrientation)&&!isShowingLandscapeView)
{
//我为此方向设置了我的新框架原点和大小
isShowingLandscapeView = YES;
}
else if(deviceOrientation == UIDeviceOrientationPortrait&& isShowingLandscapeView)
{
//我为此方向设置了我的新框架原点和尺寸
isShowingLandscapeView = NO;
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return(interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation)) ;
}


My application was not made ​​for the landscape. But when I open my YouTube channel in a UIWebView and a user launches a video, it appears in Portrait. I would like to make it appear in landscape mode if the user rotate his iPhone.

How to enable landscape mode as in this case?

I know there are "dirty hacks" to do this but I prefer something cleaner. Also I do not want the UIWebView switches to landscape but just that the videos can.

解决方案

I finally adjusted my view so that it supports landscape mode using the following code :

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation))
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait)
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = NO;
    }
}

- (void)orientationChanged:(NSNotification *)notification
{
    // We must add a delay here, otherwise we'll swap in the new view
    // too quickly and we'll get an animation glitch
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        //I set my new frame origin and size here for this orientation
        isShowingLandscapeView = NO;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

这篇关于在UIWebView中横向模式的Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 07:11