2012-06-29 33 views
6

Tôi đang sử dụng mã nhúng sau để nhúng video youtube của tôi trên IOSLàm thế nào để xoay nhúng video từ YouTube sang chế độ phong cảnh

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

Khi tôi phát video, nó chỉ phát ở chế độ potrait và không chế độ nằm ngang. Đây có phải là hạn chế do 'iframe', có cách nào xung quanh vấn đề này không?

Trả lời

2

Nó sẽ hoạt động với điều kiện UIViewController của bạn cũng có thể xoay ngang.

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

Kiểm tra xem UIViewController của bạn có thể xoay trước khi cố xoay video hay không. Nếu bạn không muốn UIViewController của bạn để có thể xoay khi video không xuất hiện trên màn hình, bạn chỉ có thể làm điều gì đó như:

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

Mã này không tồn tại trong UIViewController chính của tôi, trên thực tế, chế độ xem web để phát youtube tồn tại trong uitableviewcell. Làm thế nào để tôi xử lý định hướng của một uiwebview trong một uitable? – Zhen

+0

Bạn vẫn phải đặt mã đó bên trong UIViewController của bạn (mã có chứa UITable của bạn) –

0

Trong UIViewController của bạn, thiết lập quan điểm của mình để WebView:

[self setView: webView];

Chế độ xem web của bạn không nhận được thông báo xoay được gửi tới bộ điều khiển chế độ xem gốc. Bạn cũng có thể sử dụng phương thức addChildViewController nếu bạn đã tạo Bộ điều khiển chế độ xem riêng biệt chỉ dành cho webView của mình.

0

này là gần như giống như một câu hỏi khác tôi chỉ trả lời, Fix Notification center orientation after the app resume execution

video YouTube có có lớp UIViewController riêng mà trình bày cho họ. Họ không thực sự thực hiện - (BOOL) shouldAutorotateToInterfaceOrientation; (mà tôi biết) và vì vậy hãy sử dụng bất kỳ định hướng nào bạn đang sử dụng.

Nếu ứng dụng của bạn không quay sang phong cảnh thì ứng dụng sẽ không ở chế độ ngang. Thiết lập [UIApplication sharedApplication] .statusbarOrientation nên đặt định hướng của video Youtube, tuy nhiên nếu bạn chọn chỉ làm điều này, thay vì sau đó thực hiện đề xuất Michael Frederick's (cũng), nó có thể có một số hiệu ứng bất thường khi ra khỏi video (như giao diện người dùng) chân dung nhưng thanh trạng thái của thanh trạng thái bao trùm giao diện người dùng).

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]; 
} 
Các vấn đề liên quan