相关疑难解决方法(0)

如何检测方向变化?

我正在使用Swift,我希望能够在旋转到横向时加载UIViewController,任何人都可以指向正确的方向吗?

我在网上找不到任何东西,文档也有点困惑.

orientation-changes ios swift

142
推荐指数
15
解决办法
14万
查看次数

iOS设备的快速180度旋转会导致相机倒置

我已经实现了下面的代码来改变AVCaptureVideoSession基于以下内容的方向UIInterfaceOrientation:

- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation {
    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            return AVCaptureVideoOrientationPortrait;
        case UIInterfaceOrientationPortraitUpsideDown:
            return AVCaptureVideoOrientationPortraitUpsideDown;
        case UIInterfaceOrientationLandscapeLeft:
            return AVCaptureVideoOrientationLandscapeLeft;
        case UIInterfaceOrientationLandscapeRight:
            return AVCaptureVideoOrientationLandscapeRight;
        default:
            break;
    }
    NSLog(@"Warning - Didn't recognise interface orientation (%ld)",(long)orientation);
    return AVCaptureVideoOrientationPortrait;
}
Run Code Online (Sandbox Code Playgroud)

这段代码几乎完美无缺.但是,出现的一个问题是,如果您快速将iOS设备旋转180度,相机将显示为颠倒.

以下是旋转前相机视图的样子:

在此输入图像描述

这是旋转后的样子:

在此输入图像描述

另外,这是我的实现viewDidLayoutSubviews:

- (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    previewLayer.frame = self.view.bounds;

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:previewLayer];

    if (previewLayer.connection.supportsVideoOrientation) {
        previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
    }
}
Run Code Online (Sandbox Code Playgroud)

这个180度旋转发生时有没有人知道为什么摄像机视图会颠倒?

objective-c uiinterfaceorientation ios avcapturesession viewdidlayoutsubviews

11
推荐指数
2
解决办法
1157
查看次数