ios - 在没有 .faceUp 和 .faceDown 的情况下对方向变化使用react

标签 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。我想忽略这些方向的方向变化并且没有任何变化。 将它们放在 switchdefault: 中会导致设备恢复纵向时出现不必要的重复动画。

我确信我可以组合出一个解决方案。但我发现问题的简单性令人沮丧。

是否有一个属性仅通知 .portrait.landscapeRight.landscapeLeft 的更改。 PortraitUpsideDown

最佳答案

是的,interfaceOrientation已被弃用,现在您应该使用statusBarOrientation或traits及相关方法来检测当前方向或方向的变化。
我想向您指出这个post ,设备方向不会考虑用户可以在选项中设置的最终方向锁定。
虽然 statusBarOrientation 似乎已被弃用,但显然仅在 setter 上,如所述here来自 Apple 员工。

如何在纵向应用程序中呈现横向 View Controller ?这只是可能的示例之一:

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

这里我说的是 View 应该以纵向呈现,这将导致一个很好的方向动画, View 可以旋转,如果横向标志处于事件状态,它可以在除颠倒之外的所有方向上旋转。
要拦截方向变化,您可以:

  • 使用设备方向变化,但仅触发横向 View Controller 的呈现
  • 使用特征变化回调来触发演示,但您的应用与我的应用类似,并且您在常规设置中仅选择纵向,因此您将不会收到任何这些调用回调

在要呈现横向界面的 vc 的方向更改回调内,您可以呈现横向 View Controller :

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 的一个属性,设置后会要求开始从端口到横向的旋转。

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


请注意UIDeviceOrientationIsValidInterfaceOrientation函数,该函数非常重要,它可以释放faceup和facedown。

关于ios - 在没有 .faceUp 和 .faceDown 的情况下对方向变化使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44977656/

相关文章:

ios - SFSafariViewController 标题

ios - 将值传递给 api 时得到 nil 值

ios - 如何匹配两个TableView中的值

java - Android:仅在一个选项卡上更改方向?

iOS - 核心数据栈作为单例与主 NSManagedObjectContext

iphone - 如何在IOS中将xml文档转换为NSString

ios - Swift 3 - 滑动堆栈 View ?

ios - 在 swift 测试用例中注入(inject)单例 objective-c 类的依赖项

android - Mono for Android - 纵向所有 Activity

css - 调整导航栏纵向与横向方向的图标大小