2010-07-21 69 views
20

我的應用程序由工具欄和背景UIView中的AVCaptureVideoPreviewLayer組成。 我希望看到該工具欄旋轉與設備方向,所以在我的主視圖控制器,我實現的功能:禁用UIView的自動旋轉

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait 
      || interfaceOrientation == UIInterfaceOrientationLandscapeLeft 
      || interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

這工作得很好,但是當界面方向改變,我看到背景UIView(帶有視頻圖層)也在旋轉,所以我的視頻視圖現在左右旋轉90度......這不是很酷!
所以我的問題是:有什麼辦法禁用特定的UIView自動旋轉動畫?

我看到一個類似的問題,這個:Disabling autorotate for a single UIView,但答案不適合我的問題,我真的需要背景視圖不會移動,而不是解決問題的一種「反動畫」。所以我決定提出這個話題。

任何想法?

謝謝!

+0

我有同樣的問題。你解決了嗎?它適用於iOS 6嗎?你能提出一個完整的答案嗎?非常感謝。 – 2013-05-09 11:51:21

回答

-2

試試這個:

myVideoCaptureLayer.orientationSupported = NO; 
+5

據我所知,orientationSupported屬性是隻讀的,並告訴設備是否能夠改變視頻方向。 如果屬實,那麼可以將「orientation」屬性更改爲正確的值(AVCaptureVideoOrientationPortrait,AVCaptureVideoOrientationLandscapeLeft,...)。 感謝您讓我走向好的方向! – 2010-07-22 09:05:42

+0

哎呀!錯過了只讀部分。很高興幫助。 – christo16 2010-07-22 15:35:37

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

還是你說要禁用它只有當某個視圖顯示?如果是這樣,你可以使用UIView的isDescendantOfView方法來檢查它。

+1

嗯,我不想禁用整個界面旋轉,只是禁用它在主viewController的視圖層次結構中的特定UIView。這意味着當我旋轉設備時,我看到工具欄正在旋轉,但不是視頻視圖。 – 2010-07-21 16:45:18

+0

這不是UIView上的方法 - 它在UIViewController上。所以它會禁用整個視圖層次結構的旋轉。 – colinta 2012-11-16 15:33:51

+0

這就是我正在尋找的...... – ATOzTOA 2012-12-16 04:08:41

1

我碰到了同樣的問題,但簡單的設置上AVCaptureVideoPreviewLayer取向性是不夠的。另外的問題是CALayer不支持iPhone上的佈局管理器,所以當設備旋轉時,視頻圖層的幀可能不正確。

我通過使用自定義UIView來包含AVCaptureVideoPreviewLayer,然後覆蓋此視圖的layoutSubviews方法,以確保視頻圖層的框架總是正確設置。這也是設置方向屬性的好地方。也就是說,我的UIView看起來像:

@implementation VideoPreview 

@synthesize previewLayer; 

- (void) layoutSubviews { 
    if (previewLayer) { 
     // set the correct orientation for the video layer 
     previewLayer.orientation = [[UIDevice currentDevice] orientation]; 

     // and set its frame to that of its parent UIView 
     previewLayer.frame = self.bounds; 
    } 
} 

- (void) addPreviewLayer: (AVCaptureVideoPreviewLayer *) videoLayer { 
    previewLayer = videoLayer; 
    [self.layer addSublayer:videoLayer]; 
} 

@end 
11

這篇文章指出了正確的方向。爲了得到一個AVCaptureVideoPreviewLayer不與我包裹在一個UIView和動畫這一觀點相反的方向轉動設備:

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    float rotation; 

    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) { 
     rotation = 0; 
    } else 
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
     rotation = M_PI/2; 
    } else 
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
     rotation = -M_PI/2; 
    } 

    [UIView animateWithDuration:duration animations:^{ 
     cameraPreview.transform = CGAffineTransformMakeRotation(rotation); 
     cameraPreview.frame = self.view.frame; 
    }]; 
} 

這似乎有點迂迴,但它流暢的動畫。預覽圖層頂部的其他視圖會自動旋轉。

+0

謝謝!那爲我做了。 – KPM 2012-01-06 00:08:10

+0

我改了一下框架代碼: CGRect f = cameraPreview.frame;cameraPreview.transform = CGAffineTransformMakeRotation(rotation); cameraPreview.frame = f; – colinta 2012-11-16 15:49:45

+0

嗨,謝謝你,不會'self.view.frame'返回旋轉的視圖框架?所以如果是風景,它會返回風景畫面,如果是肖像,那麼肖像畫框等。如果我錯了,請糾正我。 – Malloc 2013-09-12 09:50:32

0

我終於找到了一個理智的答案。沒有更多的亂動與「反旋轉動畫」等!

關鍵是爲您的預覽圖層(非旋轉視圖)使用UIViewController,另一個用於實際界面。配置旋轉選項使用shouldAutorotateToInterfaceOrientation兩者:(代碼是在上述答覆):

  • 預覽視圖控制器應設置爲不旋轉
  • 接口視圖控制器應設置爲旋轉到方向你需要

重要的是要保持你的意見分開,直接添加到窗口。我在AppDelegate中執行此操作:

[self.window addSubview:previewViewController.view]; 
[self.window addSubview:interfaceViewController.view]; 
self.window.rootViewController = interfaceViewController; 
[self.window makeKeyAndVisible]; 

將您的視頻預覽置於後臺,然後將界面視圖添加到其中。該界面可自由旋轉而不會影響預覽。

需要注意的一點是:界面視圖的背景應該是透明的,否則會阻止預覽。