【问题标题】:How to rotate embedded youtube video to landscape mode如何将嵌入的 youtube 视频旋转为横向模式
【发布时间】:2012-06-30 19:56:45
【问题描述】:

我正在使用以下嵌入代码在 IOS 上嵌入我的 youtube 视频

- (NSString*)embedYouTube:(NSString*)youtube_id frame:(CGRect)frame {  
   NSString* embedHTML = @"\
   <html><head>\
   <style type=\"text/css\">\
   body {\
   background-color: transparent;\
   color: white;\
   }\
   </style>\
   </head><body style=\"margin:0\">\
   <iframe src=\"http://www.youtube.com/embed/%@?rel=0\" frameborder=\"0\" allowfullscreen width=\"%0.0f\" height=\"%0.0f\"></iframe>\
   </body></html>"; 
   NSString *html = [NSString stringWithFormat:embedHTML, youtube_id, frame.size.width, frame.size.height];

   return html;
}

//code to embed video
NSString *contentHTML;
if (currentAnswer.youtube_id != nil) {
    contentHTML = [self embedYouTube:currentAnswer.youtube_id frame:CGRectMake(CELL_TEXT_LEFT_MARGIN + CELL_AVATAR_WIDTH + CELL_SPACING, currentYAxisValue, CELL_YOUTUBEVIEW_WIDTH, CELL_YOUTUBEVIEW_HEIGHT)];
}

[webView loadHTMLString: contentHTML baseURL:nil];

当我播放视频时,它只以纵向模式播放,而不是横向模式。这是由于“iframe”造成的限制吗,有什么办法可以解决吗?

【问题讨论】:

    标签: ios youtube embed landscape uiinterfaceorientation


    【解决方案1】:
    #define RADIANS(degrees) ((degrees * M_PI) / 180.0)
    
    - (void) setTransformForCurrentOrientation {
        UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        NSInteger degrees = 0;
    
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            if (orientation == UIInterfaceOrientationLandscapeLeft) { degrees = -90; } 
            else { degrees = 90; }
        } else {
            if (orientation == UIInterfaceOrientationPortraitUpsideDown) { degrees = 180; } 
            else { degrees = 0; }
        }
    
        rotationTransform = CGAffineTransformMakeRotation(RADIANS(degrees));
    
        [UIView beginAnimations:nil context:nil];
        [webView setTransform:rotationTransform];
        [UIView commitAnimations];
    }
    

    【讨论】:

      【解决方案2】:

      这和我刚刚回答的另一个问题几乎一样,Fix Notification center orientation after the app resume execution

      YouTube 视频有自己的 UIViewController 子类来呈现它们。他们实际上并没有实现 -(BOOL)shouldAutorotateToInterfaceOrientation; (我知道),所以使用你当前的任何方向。

      如果您的应用没有旋转到横向,那么它也不会是横向的。设置 [UIApplication sharedApplication].statusbarOrientation 应该设置 Youtube 视频的方向,但是如果您选择仅执行此操作,而不是执行 Michael Frederick's 建议(以及),它可能会在播放视频时产生一些不寻常的效果(例如UI 纵向但统计栏横向覆盖 UI)。

      【讨论】:

        【解决方案3】:

        在您的 UIViewController 中,将其视图设置为 webView:

        [self setView:webView];

        您的 webview 没有收到发送到根视图控制器的旋转消息。如果您专门为您的 webView 制作了单独的 View Controller,您也可以使用 addChildViewController 方法。

        【讨论】:

          【解决方案4】:

          如果您的 UIViewController 也能够旋转到横向,它应该可以工作。

          - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
          {
              return YES;
          }
          

          在尝试旋转视频之前测试您的 UIViewController 是否能够旋转。如果您不希望您的 UIViewController 在视频不在屏幕上时能够旋转,您可以执行以下操作:

          - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
          {
              if(webView && webView.superView) return YES;
              return UIInterfaceOrientationIsPortrait(interfaceOrientation);
          }
          

          【讨论】:

          • 这段代码在我的主 UIViewController 中不存在,实际上播放 youtube 的 web 视图存在于 uitableviewcell 中。如何在 uitable 中处理 uiwebview 的方向?
          • 您仍然必须将该代码放入您的 UIViewController(包含您的 UITable 的那个)
          猜你喜欢
          • 2012-11-03
          • 1970-01-01
          • 2014-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-23
          • 2011-12-28
          • 1970-01-01
          相关资源
          最近更新 更多