2012-06-29 13 views
6

estoy usando el siguiente código para incrustar mis videos de YouTube en iOSCómo rotar incrustado youtube video a modo de paisaje

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

Cuando juego el video, sólo se juega en el modo potrait y no el modo horizontal. ¿Es esta una restricción debida a los 'iframes'? ¿Hay alguna forma de evitar esto?

Respuesta

2

Debería funcionar siempre que su UIViewController también pueda rotar al paisaje.

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

Compruebe que su UIViewController puede girar antes de intentar girar el video. Si no desea que su UIViewController sea capaz de girar cuando el vídeo no está en la pantalla, sólo podría hacer algo como:

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

Este código no existe en mi UIViewController principal, de hecho, la vista web para jugar youtube existe en una uitableviewcell. ¿Cómo manejo la orientación de una página web dentro de una comunidad? – Zhen

+0

Todavía tiene que poner ese código dentro de su UIViewController (el que contiene su UITable) –

0

En su UIViewController, fijar su vista a web View:

[self setView: webView];

Su vista web no recibe los mensajes de rotación enviados al controlador de vista raíz. También puede usar el método addChildViewController si creó un Controlador de Vista separado únicamente para su vista web.

0

Esto es casi lo mismo que otra pregunta que acaba de responder, Fix Notification center orientation after the app resume execution

vídeos de YouTube tienen su propia subclase UIViewController que los presenta. En realidad, no implementan - (BOOL) shouldAutorotateToInterfaceOrientation; (que yo sepa) y entonces use cualquier orientación en la que se encuentre actualmente.

Si su aplicación no gira al paisaje, tampoco estará en el paisaje. La configuración de [UIApplication sharedApplication] .statusbarOrientation debe establecer la orientación del video de Youtube; sin embargo, si elige hacerlo solo, en lugar de implementar la sugerencia Michael Frederick's (también), podría tener algunos efectos inusuales al salir del video (como la interfaz de usuario). retrato pero barra de estadísticas del paisaje que cubre la interfaz de usuario).

0
#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]; 
} 
Cuestiones relacionadas