禁用UIView的自动旋转

问题描述:

我的应用程序由工具栏和背景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

试试这个:

myVideoCaptureLayer.orientationSupported = NO; 
+5

据我所知,orientationSupported属性是只读的,并告诉设备是否能够改变视频方向。 如果属实,那么可以将“orientation”属性更改为正确的值(AVCaptureVideoOrientationPortrait,AVCaptureVideoOrientationLandscapeLeft,...)。 感谢您让我走向好的方向! – 2010-07-22 09:05:42

+0

哎呀!错过了只读部分。很高兴帮助。 – christo16 2010-07-22 15:35:37

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

我碰到了同样的问题,但简单的设置上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 

这篇文章指出了正确的方向。为了得到一个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

我终于找到了一个理智的答案。没有更多的乱动与“反旋转动画”等!

关键是为您的预览图层(非旋转视图)使用UIViewController,另一个用于实际界面。配置旋转选项使用shouldAutorotateToInterfaceOrientation两者:(代码是在上述答复):

  • 预览视图控制器应设置为不旋转
  • 接口视图控制器应设置为旋转到方向你需要

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

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

将您的视频预览置于后台,然后将界面视图添加到其中。该界面可*旋转而不会影响预览。

需要注意的一点是:界面视图的背景应该是透明的,否则会阻止预览。