21

我的应用程序由背景 UIView 中的工具栏和 AVCaptureVideoPreviewLayer 组成。我希望看到工具栏根据设备方向旋转,所以在我的主 ViewController 中,我实现了以下功能:

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

这工作正常,但是当界面方向改变时,我看到背景 UIView(带有视频层)也在旋转,所以我的视频视图现在向左或向右播放 90 度......这不是很酷!
所以我的问题是:有什么方法可以禁用特定 UIView 上的自动旋转动画?

我见过一个与此类似的问题:Disabling autorotate for a single UIView,但答案不适合我的问题,我真的需要背景视图不要移动,而不是用一种“计数器”来解决问题动画片”。所以我决定提出这个话题。

有任何想法吗 ?

谢谢 !

4

5 回答 5

11

这篇文章指出了正确的方向。为了让AVCaptureVideoPreviewLayer设备不旋转,我将其包裹在 aUIView中,并以相反的方向为该视图设置动画:

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

它看起来有点迂回,但动画流畅。预览层顶部的其他视图会自动旋转。

于 2011-09-02T13:24:22.660 回答
2
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return NO;
}

或者您是说仅在显示某个视图时才禁用它?如果是这样,您可以使用 UIView 的 isDescendantOfView 方法进行检查。

于 2010-07-21T16:06:57.033 回答
2

我终于找到了一个理智的答案。不再使用“反旋转动画”之类的东西!

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

  • 预览视图控制器应设置为不旋转
  • 界面视图控制器应设置为旋转到您需要的方向

重要的是通过将视图直接添加到窗口来保持视图分开。我在 AppDelegate 中执行此操作:

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

这会将您的视频预览置于后台,然后在其上添加界面视图。界面可以自由自动旋转,完全不影响预览。

需要注意的一件重要事情:界面视图的背景应该是透明的,否则会阻塞预览。

于 2013-02-26T16:28:09.303 回答
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
于 2011-04-22T11:19:06.657 回答
-2

试试这个:

myVideoCaptureLayer.orientationSupported = NO;
于 2010-07-21T17:27:10.787 回答