• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 以编程方式锁定 iDevice 方向

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:14:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

//MARK:- ORIENTATION
func changeOrientation(orientation: String) {
    switch orientation {
    case "ortrait":
        UserDefaults.standard.set("ortrait", forKey: UserDefaultsKeys.preferredOrientation)
        appDelegate.preferredOrientation = "ortrait"
        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 "ortrait":
            return .portrait
        case "Landscape":
            return .landscape
        default:
            return .all
        }
    }
}

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

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



Best Answer-推荐答案


我刚刚编写了一个示例项目来测试这是否可行。嗯,我认为是!不幸的是

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()
    }

}

关于ios - 以编程方式锁定 iDevice 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996894/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap