首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取目标UITraitCollection以及目标rect

如何获取目标UITraitCollection以及目标rect
EN

Stack Overflow用户
提问于 2021-01-02 10:11:09
回答 1查看 88关注 0票数 1

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

代码语言:javascript
复制
    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
    }

我需要这两个动画的东西正确的信息!非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-01-11 16:38:26

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

代码语言:javascript
复制
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.")
}

苹果在这里的想法是使用一个上下文参数来简化调用点,该参数可以查询多个属性,还可以执行asynchronously,这样即使在更新发生之前,最终转换的视图或VC的值也可以在一个位置被准确获取。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65535461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档