我有一个工具窗格,在紧凑的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
}我需要这两个动画的东西正确的信息!非常感谢!
发布于 2021-01-11 16:38:26
实际上,通过对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.")
}苹果在这里的想法是使用一个上下文参数来简化调用点,该参数可以查询多个属性,还可以执行asynchronously,这样即使在更新发生之前,最终转换的视图或VC的值也可以在一个位置被准确获取。
虽然您可以使用任何一种WillTransition方法执行动画,但如果可能,我会使用coordinator.animate(alongsideTransition:completion:)方法,而不是coordinator.notifyWhenInteractionChanges(_:)方法,因为它会将系统动画与您自己的自定义动画同步,并且您仍然可以使用上面的技术在context中查询新的traitCollection或frame.size。
https://stackoverflow.com/questions/65535461
复制相似问题