gpt4 book ai didi

ios - 如何强制 iOS 设备通过陀螺仪确定其方向?

转载 作者:行者123 更新时间:2023-11-30 11:11:03 26 4
gpt4 key购买 nike

我遇到的情况是,我的应用程序有时会强制特定 View Controller 以特定方向显示。

我发现执行此操作的唯一方法是手动设置设备方向,并告诉应用程序尝试像这样旋转自身:

switch (forceOrientation){
case Orientations.PORTRAIT:
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
case Orientations.LANDSCAPE:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
default:
break
}

这工作得很好。但是,一旦我关闭此 View Controller 并返回到前一个 View Controller ,无论如何握住设备,我仍然处于强制方向。有没有办法将方向值设置为“脏”或其他值并让它自动检测?

我尝试将方向值设置为 UIInterfaceOrientation.unknown.rawValue,然后尝试旋转,但这不起作用。

我使用的是 swift 4.1

-- 编辑 --

为了澄清我在做什么:

当我的 View Controller 被创建时,它将自己设置为 Root View Controller (这样它的旋转设置将真正起作用,否则使用根行为)在它初始化期间的某个时刻,可以告诉它应该以特定的方式进行导向。如果是这样,那么它会执行上面的代码,并适本地设置它的方向掩码。我正在使用这些覆盖:

public override var shouldAutorotate: Bool{
return allowOrientationChange // this is set at some point after initializing
}

public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return orientationMask // this defaults to orientationMaskAll but is overridden
}

关闭 View Controller 时,之前的根 Controller 将作为根添加回来。 (但它仍然会旋转到强制的位置,并且除非您移动设备,否则不会更新)

最佳答案

您可以覆盖 var supportInterfaceOrientations: UIInterfaceOrientationMask { get } 并以在任何给定时间返回您想要的可用方向的方式进行设置。

基本上,让此属性返回一个与您的 forceOrientation 枚举相关的变量。

参见https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations了解详情。

要强制旋转,您可以这样做:

let previousOrientation = UIDevice.current.orientation  
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
//Rotates all view controllers
UIViewController.attemptRotationToDeviceOrientation()

//Find all UIViewControllers that need to be locked, I recommend using a protocol


guard let appDelegate = UIApplication.sharedApplication().delegate,
let window = appDelegate.window,
let root = window.rootViewController,


else{
fatalError("error")
}
var viewControllers : [UIViewController] = []()
if root.presentedViewController != nil {
viewControllers += [root.presentedViewController]
}
viewControllers += root.children
viewControllers.filter{$0 as OrientationLocked}.forEach{ vc in
vc.orientationMask = ... //whatever previousOrientation isn't
}

//undo rotation
UIDevice.current.setValue(previousOrientation, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

//View controllers that had been filtered are now rotated, where as anything underneath were treated as if a rotation did not happen

... 任何可以锁定的 View Controller 都需要这个协议(protocol)

protocol OrientationLocked {
var orientationMask : UIInterfaceOrientationMask
}

关于ios - 如何强制 iOS 设备通过陀螺仪确定其方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172482/

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