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

Pra*_*I V 12 ios scenekit arkit arscnview

我正在使用ARKit,我想允许用户在纵向和横向模式下使用该应用程序.我希望所有UI控件在方向更改时旋转,除了ARSCNView.我试图在相反的方向转换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)

    }
Run Code Online (Sandbox Code Playgroud)

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

Mam*_*yya 1

您无法根据视图指定设备轮换规则。它必须在视图控制器的基础上设置。这就是 iOS 的工作原理。因此,要实现您所需要的,您必须自己处理。例如,如果您将其显示ARSCNView为全屏视图,则可以将其显示在自定义UIViewController子类中,并设置该控制器的旋转配置。

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


方法#1:

UIViewController您可以通过覆盖应用程序委托的方法来设置任何支持的视图方向application:supportedInterfaceOrientationsForWindow:

示例代码:

// 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
}
Run Code Online (Sandbox Code Playgroud)

参考:

application:supportedInterfaceOrientationsForWindow:方法文档


方法#2:

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

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return self.topViewController?.supportedInterfaceOrientations ?? .all
}
Run Code Online (Sandbox Code Playgroud)

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

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

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}
Run Code Online (Sandbox Code Playgroud)

参考:

supportedInterfaceOrientations财产文件


方法#3:

如果您不想UINavigationController在上面的方法 #2 中对您的子类进行子类化,您可以将您的设置设置SomeViewController为实现的导航控制器委托navigationControllerSupportedInterfaceOrientations(:)

示例代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // ...

    self.navigationController?.delegate = self
}

// MARK: - UINavigationControllerDelegate

func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
    return navigationController.topViewController?.supportedInterfaceOrientations ?? .all
}
Run Code Online (Sandbox Code Playgroud)

参考:

马特的回答在这里