【问题标题】:How to prevent ARSCNView from rotating during orientation change如何防止 ARSCNView 在方向更改期间旋转
【发布时间】:2018-03-15 06:22:34
【问题描述】:

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

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 会话的场景视图旋转,同时允许所有其他 UI 控件在方向更改时旋转?

【问题讨论】:

  • @stackoverflow.com/users/1015208/praveen-gowda-i-v 你解决了吗?我正在寻找相同的..
  • @yaali 不,找不到任何东西来实现它
  • 这方面有什么更新吗?
  • 有一次我看到旋转神秘地开始工作,但是当我刚刚重新启动应用程序时,它又被破坏了(代码中没有任何改变)。我无法重现这个,所以我想这毕竟是一个错误。

标签: ios scenekit arkit arscnview


【解决方案1】:

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

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


方法#1:

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

示例代码:

// 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
}

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

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

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

参考:

supportedInterfaceOrientations property documentation


方法#3:

如果您不想像上面的方法 #2 那样对您的 UINavigationController 进行子类化,您可以将您的 SomeViewController 设置为实现 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

【讨论】:

  • 这对我的问题不起作用。我想只阻止ARSCNView 旋转。您的建议仅适用于每个视图控制器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
相关资源
最近更新 更多