Disabling auto rotation for a UIView

前端 未结 5 1837
谎友^
谎友^ 2021-02-05 07:10

My application is composed of a toolbar and an AVCaptureVideoPreviewLayer in a background UIView. I\'d like to see that toolbar rotate regarding the device orientation, so in my

5条回答
  •  名媛妹妹
    2021-02-05 07:44

    This post pointed in the right direction. To get an AVCaptureVideoPreviewLayer to not rotate with the device I wrapped it in a UIView and animated that view in the opposite direction:

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

    It seems a little roundabout but it animates smoothly. Other views on top of the preview layer automatically rotate as they should.

提交回复
热议问题