Preventing AVCaptureVideoPreviewLayer from rotating, but allow UI layer to rotate with orientation

后端 未结 4 671
半阙折子戏
半阙折子戏 2020-12-16 07:26

I have two view controllers. One is the root VC and contains the UI interface such as the record button. On this view controller, I also display the view of another VC at in

4条回答
  •  太阳男子
    2020-12-16 07:48

    Make sure to set shouldAutorotate to return false:

    -(BOOL)shouldAutorotate{
        return NO;
    }
    

    register for Notifications that orientation changed:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    

    implement the notification change

    -(void)orientationChanged:(NSNotification *)notif {
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    
    // Calculate rotation angle
    CGFloat angle;
    switch (deviceOrientation) {
        case UIDeviceOrientationPortraitUpsideDown:
            angle = M_PI;
            break;
        case UIDeviceOrientationLandscapeLeft:
            angle = M_PI_2;
            break;
        case UIDeviceOrientationLandscapeRight:
            angle = - M_PI_2;
            break;
        default:
            angle = 0;
            break;
    }
    
    
    }
    

    and rotate the UI

     [UIView animateWithDuration:.3 animations:^{
            self.closeButton.transform = CGAffineTransformMakeRotation(angle);
            self.gridButton.transform = CGAffineTransformMakeRotation(angle);
            self.flashButton.transform = CGAffineTransformMakeRotation(angle);
        } completion:^(BOOL finished) {
    
    }];
    

    This is how I implement the screen being locked but rotating the UI, if this works link the stacks post and I can copy it over there and you can tick it :P

提交回复
热议问题