AVCaptureVideoPreviewLayer smooth orientation rotation

后端 未结 9 905
Happy的楠姐
Happy的楠姐 2021-02-02 16:45

I\'m trying to disable any discernable orientation rotation to an AVCaptureVideoPreviewLayer while still maintaining rotation for any subviews. AVCaptureVideoPreviewLayer does h

9条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 16:56

    Using an affine transform on the view containing the preview layer creates a smooth transition:

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        if (cameraPreview) {
            if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
                cameraPreview.transform = CGAffineTransformMakeRotation(0);
            } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
                cameraPreview.transform = CGAffineTransformMakeRotation(M_PI/2);
            } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
                cameraPreview.transform = CGAffineTransformMakeRotation(-M_PI/2);
            }
            cameraPreview.frame = self.view.bounds;
        }
    }
    

    The willAnimateRotationToInterfaceOrientation function is called within the rotation animation block so changing properties here will animate along with the rest of the rotation animations. This function has been phased out in iOS 6 and replaced with new methods to handle device rotation, so this works for now, but isn't the best solution.

提交回复
热议问题