gpt4 book ai didi

ios - 如何防止 ARSCNView 在方向更改期间旋转

转载 作者:技术小花猫 更新时间:2023-10-29 10:40:10 24 4
gpt4 key购买 nike

我正在使用 ARKit,我希望允许用户在纵向和横向模式下使用该应用程序。除了 ARSCNView 之外,我希望所有 UI 控件都在方向更改时旋转。我试图将 sceneView 变换到相反的方向,但没有成功。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

let targetRotation = coordinator.targetTransform
let inverseRotation = targetRotation.inverted()

coordinator.animate(alongsideTransition: { (context) in
self.sceneView.transform = self.sceneView.transform.concatenating(inverseRotation)
context.viewController(forKey: UITransitionContextViewControllerKey.from)
}, completion: nil)

}

如何防止 ARKit session 的场景 View 旋转,同时允许所有其他 UI 控件在方向更改时旋转?

最佳答案

您不能在 View 基础上指定设备旋转规则。它必须在 View Controller 的基础上设置。这就是 iOS 的工作原理。因此,要实现您的需要,您必须自己处理。例如,如果您将 ARSCNView 显示为全屏 View ,则可以在自定义 UIViewController 子类中显示它,并为其设置旋转配置 Controller 。

为特定 View Controller 设置支持的 View 旋转可以通过多种方式实现,下面是其中的一些。


方法#1:

您可以通过重写应用委托(delegate)的方法 application:supportedInterfaceOrientationsForWindow: 为任何 UIViewController 设置支持的 View 方向。

示例代码:

// this goes into your AppDelegate...
// Basically, this checks the current visible view controller type and decide
// what orientations your app supports based on that view controller type (class)
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
let visibleViewController = self.topViewController(withRootViewController: window?.rootViewController)

// Support only portrait orientation for a specific view controller
if visibleViewController is SomeViewController {
return .portrait
}

// Otherwise, support all orientations (standard behaviour)
return .allButUpsideDown
}

// This simple helper method is to extract the exact visible view controller at
// the moment, as `window.rootViewController` could have some container controller
// like `UINavigationController` or so that holds more controllers into it
private func topViewController(withRootViewController rootViewController: UIViewController?) -> UIViewController? {
if let rootViewController = rootViewController as? UITabBarController {
return rootViewController.selectedViewController
} else if let rootViewController = rootViewController as? UINavigationController {
return rootViewController.visibleViewController
} else if let presentedViewController = rootViewController?.presentedViewController {
return topViewController(withRootViewController: presentedViewController)
}
return rootViewController
}

引用:

application:supportedInterfaceOrientationsForWindow: method documentation


方法 #2:

子类化您的 UINavigationController 并覆盖以下属性:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self.topViewController?.supportedInterfaceOrientations ?? .all
}

现在这总是查看您的 View Controller 的 supportedInterfaceOrientations 并根据返回值设置支持的方向。这样做可以让您在任何您想要的 View Controller 中简单地覆盖它,设置一些自定义值。

例如,在 SomeViewController 中,您可以简单地添加:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}

引用:

supportedInterfaceOrientations property documentation


方法#3:

如果您不想像上面的方法 #2 那样子类化您的 UINavigationController,您可以将您的 SomeViewController 设置为实现 的导航 Controller 委托(delegate)navigationControllerSupportedInterfaceOrientations(:)

示例代码:

override func viewDidLoad() {
super.viewDidLoad()

// ...

self.navigationController?.delegate = self
}

// MARK: - UINavigationControllerDelegate

func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
return navigationController.topViewController?.supportedInterfaceOrientations ?? .all
}

引用:

Matt's answer here

关于ios - 如何防止 ARSCNView 在方向更改期间旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555009/

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