2013-08-23 67 views

回答

1

其實Youtube的界面並沒有真正旋轉,他們只是呈現視頻層全屏和旋轉圖層。

當您旋轉設備時,會發生同樣的想法,它們會使視頻圖層填滿屏幕並根據設備旋轉進行旋轉。 Facebook在全屏查看照片時旋轉設備並旋轉視圖時也是如此。

你可以開始通過詢問生成裝置符號定向通知監控旋轉:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification 
    object:[UIDevice currentDevice]]; 

然後在-(void)orientationChanged:方法更改UI:

- (void) orientationChanged:(NSNotification *)note 
{ 
    UIDevice * device = note.object; 

    CGAffineTransform transfrom; 
    CGRect frame = self.videoView.frame; 

    switch(device.orientation) 
    { 
     case UIDeviceOrientationPortrait: 
     case UIDeviceOrientationPortraitUpsideDown:  
      transfrom = CGAffineTransformIdentity; 
      frame.origin.y = 10.0f; 
      frame.origin.x = 10.0f; 
      frame.size.width = [UIScreen mainScreen] bounds].size.width; 
      frame.size.height = 240.0f; 

     break; 

     case UIDeviceOrientationLandscapeLeft: 
      transfrom = CGAffineTransformMakeRotation(degreesToRadians(90)); 
      frame.origin.y = 0.0f; 
      frame.origin.x = 0.0f; 
      frame.size.width =[UIScreen mainScreen] bounds].size.height; 
      frame.size.height =[UIScreen mainScreen] bounds].size.width; 
     break; 

     case UIDeviceOrientationLandscapeRight: 
      transfrom = CGAffineTransformMakeRotation(degreesToRadians(-90)); 
      frame.origin.y = 0.0f; 
      frame.origin.x = 0.0f; 
      frame.size.width =[UIScreen mainScreen] bounds].size.height; 
      frame.size.height =[UIScreen mainScreen] bounds].size.width; 
      break; 

     default: 
     return; 
     break; 
    }; 


    [UIView animateWithDuration:0.3f animations: ^{ 
     self.videoView.frame = frame; 
     self.videoView.transform = transfrom; 
    }]; 

} 

此代碼是未經測試寫入只是在這裏給你一個關於它如何可以做的概念。

+0

你有什麼想法怎麼辦它? – chetu

+0

當我旋轉設備時,視圖正在旋轉,但在按鈕上單擊如何旋轉我不知道的視圖。 – chetu

+2

這將是最乾淨,最好的方式來做到這一點,但實際上不是如何Youtube,Facebook或任何這些傢伙處理它。只旋轉圖層意味着底層的UIViewController仍處於縱向模式。然後,顯示一個系統UIAlertView,UIActionSheet,甚至鍵盤都會讓它們以縱向顯示。爲了讓它們在橫向出現,UIViewController也應該處於橫向模式。這就是爲什麼@ sumit-mundra的答案實際上是一個更有效的答案,即使hacky ... – Zedenem

4

試試這個 首次導入此: #import <objc/message.h>

比你的按鈕使用方法UIView的縱向和橫向的這

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) 
{ 

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
    { 
     objc_msgSend([UIDevice currentDevice],@selector(setOrientation:),UIInterfaceOrientationLandscapeLeft); 
    }else 
    { 
     objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientation); 

    } 

} 
+0

謝謝,你能解釋一下我的代碼嗎?它是如何工作的? – chetu

相關問題