gpt4 book ai didi

ios - 防止 AVCaptureVideoPreviewLayer 旋转,但允许 UI 层随方向旋转

转载 作者:可可西里 更新时间:2023-11-01 04:10:44 36 4
gpt4 key购买 nike

我有两个 View Controller 。一个是根VC,包含录制按钮等UI界面。在这个 View Controller 上,我还在索引 0 处显示另一个 VC 的 View 。这个 View 包含一个 AVCaptureVideoPreviewLayer。

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

知道怎么做吗?我发现了一个建议,建议将预览添加到应用程序委托(delegate)的窗口,因为它不符合导航 Controller 的旋转,但它对我不起作用。

谢谢!

最佳答案

我也有类似的情况。我只有一个 View Controller ,我想要一个不在其中旋转的 AVCaptureVideoPreviewLayer。我发现@SeanLintern88 接受的解决方案对我不起作用;状态栏从未移动,屏幕上的 WKWebView 未正确调整大小。

我遇到的一个更大的问题是我将我的 AVCaptureVideoPreviewLayer 放在 View Controller 的 View 中。最好创建一个新的 UIView 来保存图层。

之后我找到了苹果的技术说明QA1890: Preventing a View From Rotating .这使我能够生成以下快速代码:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
coordinator.animateAlongsideTransition(
{ (UIViewControllerTransitionCoordinatorContext) in
let deltaTransform = coordinator.targetTransform()
let deltaAngle = atan2f(Float(deltaTransform.b), Float(deltaTransform.a))
var currentRotation : Float = (self.previewView!.layer.valueForKeyPath("transform.rotation.z")?.floatValue)!
// Adding a small value to the rotation angle forces the animation to occur in a the desired direction, preventing an issue where the view would appear to rotate 2PI radians during a rotation from LandscapeRight -> LandscapeLeft.
currentRotation += -1 * deltaAngle + 0.0001;
self.previewView!.layer.setValue(currentRotation, forKeyPath: "transform.rotation.z")
self.previewView!.layer.frame = self.view.bounds
},
completion:
{ (UIViewControllerTransitionCoordinatorContext) in
// Integralize the transform to undo the extra 0.0001 added to the rotation angle.
var currentTransform : CGAffineTransform = self.previewView!.transform
currentTransform.a = round(currentTransform.a)
currentTransform.b = round(currentTransform.b)
currentTransform.c = round(currentTransform.c)
currentTransform.d = round(currentTransform.d)
self.previewView!.transform = currentTransform
})
}

原始技术说明中没有 self.previewView!.layer.frame = self.view.bounds 这行,但我发现这是非常必要的,因为虽然 anchor 没有移动,框架有。如果没有那条线,预览将发生偏移。

此外,由于我所做的所有工作都是将 View 保持在正确的位置,因此我必须移除对它的所有定位约束。当我加入它们时,它们会导致预览向相反的方向偏移。

关于ios - 防止 AVCaptureVideoPreviewLayer 旋转,但允许 UI 层随方向旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202597/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com