在没有.faceUp和.faceDown

时间:2017-07-07 18:39:03

标签: ios swift orientation

当设备处于纵向/横向模式时,我有一个UIView出现/消失并带有动画。

我这样做:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationChanged), name: Notification.Name.UIDeviceOrientationDidChange, object: nil)
}

func deviceOrientationChanged() {
        switch UIDevice.current.orientation {
        case .portrait:
            //appear
        case .landscapeLeft, .landscapeRight:
            //disappear
        case default: 
            //appear; here lies the problem
        }
    }

问题是.faceDown.faceUp。我想忽略这些方向的方向变化而没有任何改变。 将它们置于default:的{​​{1}}会导致设备重新成像时无法重复播放动画。

我相信我可以合并解决方案。但我发现问题的简单性令人沮丧。

是否有一项属性仅通知switch.portrait.landscapeRight.landscapeLeft的更改?

1 个答案:

答案 0 :(得分:1)

赞成interfaceOrientation已被弃用,现在您应该使用statusBarOrientation或特征及相关方法来检测当前方向或方向更改。
我想指向此post,设备方向不会考虑用户可以在选项中设置的最终方向锁定。
虽然statusBarOrientation似乎已被弃用,但显然仅限于Apple员工所述的here设置者。

你怎么能在肖像应用程序中呈现横向视图控制器?这只是一个可能的例子:

// ApplicationDelegate overridden method
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if topController() is FullGraphViewController {
            return .all
        }  else {
            return .portrait
        }
    }

基本上我说当呈现FullGraphViewController时,它可以以所有可能的方向显示。

// FullgraphViewController overridden methods
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if landActive {
            return [UIInterfaceOrientationMask.allButUpsideDown]
        }
        return [UIInterfaceOrientationMask.portrait]
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }

我在这里说视图应该以纵向显示,这将导致一个漂亮的方向动画,视图可以旋转,如果横向标志处于活动状态,它可以在所有方向旋转,除了颠倒。
要拦截方向更改,您可以:

  • 使用设备方向更改,但仅触发横向视图控制器的演示文稿
  • 使用特征变体回调来触发演示文稿,但您的应用程序就像我的应用程序,并且您在一般设置中只选择纵向方向,您将不会收到任何这些调用回调

在方向内更改了要显示横向界面的vc的回调,您可以呈现您的横向视图控制器:

func orientationChanged(_ notification: Notification) {
    let orientation = UIDevice.current.orientation
    if UIDeviceOrientationIsValidInterfaceOrientation(orientation) && UIDeviceOrientationIsLandscape(orientation) {
        let vc = storyboard!.instantiateViewController(withIdentifier: Constants.ViewControllerIdentifiers.LandscapeFullGraphViewController) as! FullGraphViewController
        vc.title = navigatorViewController?.navigationBarView.barTitle
        vc.instrumentInfo = instrumentInfo
        vc.modalTransitionStyle = .crossDissolve
        present(vc, animated: true) {
            vc.landActive = true
        }
    }
}

landActive是FullGraphViewController的一个属性,当设置为when时,要求从端口到横向的旋转开始。

var landActive = true {
        didSet(old) {
            UIViewController.attemptRotationToDeviceOrientation()
            view.setNeedsLayout()
            view.layoutIfNeeded()
        }
    }


请注意功能 UIDeviceOrientationIsValidInterfaceOrientation ,这非常重要,可以面朝上和面朝下放电。