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

标签 ios scenekit arkit arscnview

我正在使用 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/

相关文章:

xcode - dyld : Library not loaded:/System/Library/Frameworks/SceneKit. 框架/SceneKit

ios - 将 SCNNodes 设置为具有恒定的 y 轴值?

swift - 获取放置的 SCNNode 的世界 x、y 和 z - Swift

ios - 使用 GDataXML 将 XML 元素移动到不同的位置

ios - swift 中的 3D 立方体转换 View

ios - 如何制作像gomoji应用程序一样的2D模型(可移动)?

ios - SceneKit——在 SCNNode 面对的方向上施加力

ios - ARKit图像识别和文字显示

ios - 如何通过iOS应用程序在Facebook上分享应用程序信息?

ios - 您如何对 iPhone 应用进行 Beta 测试?