禁用UIView的自动旋转

Tho*_*ert 21 iphone uiview uiinterfaceorientation

我的应用程序由后台UIView中的工具栏和AVCaptureVideoPreviewLayer组成.我想看看关于设备方向的工具栏旋转,所以在我的主ViewController中,我实现了这个功能:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait
            || interfaceOrientation == UIInterfaceOrientationLandscapeLeft
            || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Run Code Online (Sandbox Code Playgroud)

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

我已经看到了一个类似的问题:为单个UIView禁用自动旋转,但答案不适合我的问题,我真的需要背景视图不要移动,不要用一种"计数器来解决问题动画".所以我决定提出这个话题.

有任何想法吗 ?

谢谢 !

Ant*_*tox 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;
    }];
}
Run Code Online (Sandbox Code Playgroud)

它似乎有点回旋,但它动画很顺利.预览图层顶部的其他视图会自动旋转.


chr*_*o16 -2

尝试这个:

myVideoCaptureLayer.orientationSupported = NO;
Run Code Online (Sandbox Code Playgroud)

  • 据我了解,orientationSupported 属性是只读的,它告诉设备是否能够更改视频方向。如果为真,则可以将“orientation”属性更改为正确的值(AVCaptureVideoOrientationPortrait、AVCaptureVideoOrientationLandscapeLeft,...)。感谢您让我朝着好的方向前进! (5认同)