【问题标题】:How to get target UITraitCollection as well as target rect如何获取目标 UITraitCollection 以及目标 rect
【发布时间】:2021-01-02 02:11:08
【问题描述】:

我有一个工具窗格,在紧凑型 H 模式下,它将位于整个屏幕的底部,但在紧凑型 V 模式(或非紧凑型 H 模式)下,它将作为浮动窗格位于右侧。如何获得目标 UITraitCollection + 目标大小?它们似乎有两种不同的方法:

    override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
        // need size rect
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        // need traits
    }

我需要这两个信息来正确地制作动画!非常感谢!

【问题讨论】:

  • 嘿@Mike S,我的解决方案有效吗?

标签: swift uikit uitraitcollection


【解决方案1】:

实际上,通过对UIViewControllerTransitionCoordinator 进行操作,您可以在这两种方法中获取值、大小和特征集合。

let didQueueAnimation = coordinator.animate(alongsideTransition: { context in
    guard let view = context.view(forKey: UITransitionContextViewKey.to) else { 
        return
    }
    let newScreenSize = view.frame.size
    guard let viewController = context.viewController(forKey: UITransitionContextViewKey.to) else { 
        return
    }
    let newTraitCollection = viewController.traitCollection // <-- New Trait Collection
    // You can also get the new view size from the VC:
    // let newTraitCollection = viewController.view.frame.size

    // Perform animation if trait collection and size match.

}, completion: { context in
    // Perform animation cleanup / other pending tasks.
})

if (didQueueAnimation) {
    print("Animation was queued.")
}

Apple在这里的想法是用一个可以查询多个属性的上下文参数来简化调用站点,并且还可以异步执行,以便可以准确获取最终转换后的视图或VC的值甚至在更新发生之前就在一个地方。

虽然您可以在任一WillTransition 方法中执行动画,但如果可能,我会使用coordinator.animate(alongsideTransition:completion:) 方法而不是coordinator.notifyWhenInteractionChanges(_:),因为它将系统动画与您自己的自定义动画同步,您仍然可以查询@ 987654326@ 用于新的traitCollectionframe.size 使用上述技术。

【讨论】:

    猜你喜欢
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多