AVCaptureVideoPreviewLayer smooth orientation rotation

后端 未结 9 891
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:59

    You should use CATransform3DMakeRotation which is meant for Layers, like it has been mentioned on the comments:

    For instance:

    float degreesToRotate = 90.0; //Degrees you want to rotate
    self.previewLayer.transform = CATransform3DMakeRotation(degreesToRotate / 180.0 * M_PI, 0.0, 0.0, 1.0);
    

    In my case, being "self.previewLayer" the layer to rotate. Have in mind that you should clip it to it's container view's bounds afterwards:

    self.previewLayer.frame = self.view.bounds;
    

    . . .

    EDIT: If you are going to rotate as a result of the device being rotated (willAnimateRotationToInterfaceOrientation) you should make the transformation like this:

    [CATransaction begin];
        [CATransaction setAnimationDuration:duration];
        [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        //**Perform Transformation here**
        [CATransaction commit];
    

    This way, the layer will rotate as the view does.

提交回复
热议问题