我正在尝试在 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/