iphone - iOS 6 上的 YouTube 播放器方向问题

标签 iphone ios

我正在尝试在 UIWebView 中播放 YouTube 视频(参见下面的代码)。

一切正常,但 YouTube 播放器不支持 iOS 6 上的方向更改。我的整个应用仅处于纵向模式。

我该如何解决这个问题?任何帮助表示赞赏。

我使用的代码如下:

- (void)viewDidLoad

{        
   [super viewDidLoad];
   float width = 300.0f;
   float height = 300.0f;
   youTubeURL = @"http://www.youtube.com/embed/ZiIcqZoQQwg"; 
   UIWebView *wv = [[UIWebView alloc] init];
   wv.frame = CGRectMake(10, 80, width, height);

   NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
   [html appendString:@"<html><head>"];
   [html appendString:@"<style type=\"text/css\">"];
   [html appendString:@"body {"];
   [html appendString:@"background-color: black;"];
   [html appendString:@"color: white;"];
   [html appendString:@"}"];
   [html appendString:@"</style>"];
   [html appendString:@"</head><body style=\"margin:0\">"];
   [html appendFormat:@"<embed id=\"yt\" src=\"%@\"", youTubeURL];
   [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", 300.0f, 300.0f];
   [html appendString:@"</body></html>"];
   [wv loadHTMLString:html baseURL:nil];
   [self.view addSubview:wv];  
}

最佳答案

您需要做的如下...

首先在 viewdidLoad 中发出一个通知,当您旋转设备时将调用该通知..

-(void)ViewDidLoad
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(rotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];
}

现在通知将调用名为“rotate:”的方法,因此我们必须像下面那样实现该方法。

#pragma mark - Rotate Screen Method
- (void)rotate:(NSNotification *)n {

switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationLandscapeLeft:
        yourwebview.transform = CGAffineTransformMakeRotation(M_PI / 2);
        yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame
        break;
    case UIDeviceOrientationLandscapeRight:
        yourwebview.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        yourwebview.frame = CGRectMake(0, 0,768, 1024);//you can set any frame
        break;
    case UIDeviceOrientationPortrait:
        yourwebview.transform = CGAffineTransformIdentity;
        yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        yourwebview.transform = CGAffineTransformMakeRotation(M_PI);
        yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame
        break;
    default:
        break;
   }
 }

就是这样。

关于iphone - iOS 6 上的 YouTube 播放器方向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955116/

相关文章:

iphone - 如何在cocos2d iPhone中录制游戏

iphone - Objective-C/自动调整 subview 不起作用

ios - 调试父类(super class)或协议(protocol)扩展/实现时调试嵌入式框架未按预期工作

iPhone 配置实用程序 - 为 webapp 添加 webclip

ios - 使用 Chrome DevTools 调试 iOS 6+7 Mobile Safari

ios - 如何在 Objective-C 中向 iOS healthkit 应用程序添加正念时间?

ios - 如何将一个节点附加到另一个节点?

objective-c - 将单元格添加到 UITableView

iphone - 苹果推送通知-无法回调didRegisterForRemoteNotificationsWithDeviceToken

iphone - 为什么我在使用 PayPal API 时缺少符号 "_xmlNodeListGetString"?