防止AVCaptureVideoPreviewLayer旋转,但允许UI图层按方向旋转 [英] Preventing AVCaptureVideoPreviewLayer from rotating, but allow UI layer to rotate with orientation

查看:318
本文介绍了防止AVCaptureVideoPreviewLayer旋转,但允许UI图层按方向旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器。一个是根VC,包含UI界面,如记录按钮。在这个视图控制器上,我还在索引0处显示另一个VC的视图。该视图包含AVCaptureVideoPreviewLayer。

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 index 0. This view contains a AVCaptureVideoPreviewLayer.

我希望我的摄像机模仿Apple摄像机应用程序,界面布局随旋转调整,但视频预览图层不调整。你可以看到股票视频应用程序中的录制计时器(UILabel)如何消失,并根据方向重新出现在顶部。

I would like my video camera to mimic the Apple video camera app, where the interface layout adjusts with the rotation, but the video preview layer does not. You can see how the recording timer (UILabel) in the stock video app disappears and reappears at the top depending on the orientation.

任何想法如何做到这一点?我发现了一个建议,建议将预览添加到app delegate的窗口,因为它不符合导航控制器的旋转,但它对我不起作用。

Any idea how to do this? I found one suggestion that recommendeds adding the preview to the app delegate's window, since it won't conform to the rotation of the nav controller, but it didn't work for me.

谢谢!

推荐答案

确保将shouldAutorotate设置为返回false :

Make sure to set shouldAutorotate to return false:

-(BOOL)shouldAutorotate{
    return NO;
}

注册方向更改的通知:

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

实施通知变更

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


}

并旋转UI

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

}];

这是我实现屏幕被锁定但旋转UI的方式,如果这样可以链接堆栈帖子我可以在那里复制它,你可以勾选它:P

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

这篇关于防止AVCaptureVideoPreviewLayer旋转,但允许UI图层按方向旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆