以编程方式锁定iDevice方向

妮娜

在我们正在开发的应用程序中,我们有一个选项可以让用户选择首选方向(即,如果他们选择Portrait将应用程序锁定为纵向,并且相同,Landscape并且如果Both选择了该应用程序则可以在所有方向上使用),我正在分享我尝试过的代码,但我不确定此功能是否完全可行

//MARK:- ORIENTATION
func changeOrientation(orientation: String) {
    switch orientation {
    case "Portrait":
        UserDefaults.standard.set("Portrait", forKey: UserDefaultsKeys.preferredOrientation)
        appDelegate.preferredOrientation = "Portrait"
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        break
    case "Landscape":
        UserDefaults.standard.set("Landscape", forKey: UserDefaultsKeys.preferredOrientation)
        appDelegate.preferredOrientation = "Landscape"
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        break
    default:
        UserDefaults.standard.set("Both", forKey: UserDefaultsKeys.preferredOrientation)
        appDelegate.preferredOrientation = "Both"
        break
    }
    /*not necessary*/
    let vc = UIViewController()
    UIViewController.attemptRotationToDeviceOrientation()//forces to rotate
    /*not necessary*/
    self.present(vc, animated: false, completion: nil)
    UIView.animate(withDuration: 0.3, animations: {
        vc.dismiss(animated: false, completion: nil)
    })
    /*not necessary*/
}

open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    get {
        switch appDelegate.preferredOrientation {
        case "Portrait":
            return .portrait
        case "Landscape":
            return .landscape
        default:
            return .all
        }
    }
}

open override var shouldAutorotate: Bool {
    get {
        return true
    }
}

但是,如果在人像模式下选择“风景”,它将自动切换为风景。但是,如果我将设备旋转回纵向,则它也可以正常工作(按照要求不应该工作)。要求类似于我们仅以纵向模式设置项目时发生的情况以及将设备旋转到横向模式时的行为。

двп.петров

我只是编写了一个示例项目来测试是否可行。好吧,我认为是!很遗憾

UIViewController.attemptRotationToDeviceOrientation()

不能像我希望的那样神奇地完成这项工作-比这复杂得多。请掠夺以下代码。您需要的所有魔术都在动作中发生forceChangeOrientations

    class ViewController: UIViewController {

    enum Orientation {
        case Landscape
        case Portrait

        mutating func changeOrientation() {
            if self == .Portrait {
                self = .Landscape
            }
            else {
                self = .Portrait
            }
        }

        func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
            switch self {
            case .Landscape:
                return .Landscape
            case .Portrait:
                return .Portrait
            }
        }

        func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
            switch self {
            case .Landscape:
                return UIInterfaceOrientation.LandscapeLeft
            case .Portrait:
                return .Portrait
            }
        }
    }

    var currentOrientation: Orientation = .Portrait

    //    Returns a Boolean value indicating whether the view controller's contents should auto rotate.
    override func shouldAutorotate() -> Bool {
        return true
    }

//    Returns all of the interface orientations that the view controller supports.
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return currentOrientation.supportedInterfaceOrientations() //UIInterfaceOrientationMask.All
    }

//    Returns the interface orientation to use when presenting the view controller.
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

    @IBAction func forceChangeOrientations(sender: AnyObject) {
        self.currentOrientation.changeOrientation()

        let value = self.currentOrientation.preferredInterfaceOrientationForPresentation().rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
        UIViewController.attemptRotationToDeviceOrientation()
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章