[英]Animate root view controller embedded into a navigation controller presented modally
I need to animate a UIViewController which is the root of a UINavigationController.我需要为 UIViewController 制作动画,它是 UINavigationController 的根。 This navigation controller is presented modally.此导航 controller 以模态方式呈现。 I know I can do it with the lifecycle methods of the view controller, but I want to use a UIViewControllerAnimatedTransitioning
if possible to keep the VC code clean.我知道我可以使用视图 controller 的生命周期方法来做到这一点,但如果可能的话,我想使用UIViewControllerAnimatedTransitioning
来保持 VC 代码的清洁。
I tried the following:我尝试了以下方法:
a) Set the UINavigationControllerDelegate
to the navigation controller, so I could use this delegate function: a) 将UINavigationControllerDelegate
设置为导航 controller,所以我可以使用这个委托 function:
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
However, it only gets called during transitions within the navigation controller (from VC to VC).但是,它仅在导航 controller(从 VC 到 VC)中的转换期间被调用。
b) Set a UIViewControllerTransitioningDelegate
in the root view controller and use this delegate function: b) 在根视图 controller 中设置UIViewControllerTransitioningDelegate
并使用此委托 function:
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
But again, it doesn't get called as the one presenting is the navigation controller.但同样,它不会被称为导航 controller。
c) Set a UIViewControllerTransitioningDelegate
in the navigation controller and use the same delegate function: c) 在导航 controller 中设置一个UIViewControllerTransitioningDelegate
并使用相同的委托 function:
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
In this case, it gets called and I can inject the UIViewControllerAnimatedTransitioning
object, but I can only think about hacks to animate the root, eg在这种情况下,它会被调用,我可以注入UIViewControllerAnimatedTransitioning
object,但我只能考虑为根设置动画,例如
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: .from),
let toVC = transitionContext.viewController(forKey: .to) as? UINavigationController,
toVC.children.count == 1,
let myRootVC = toVC.children.first as? MyViewController
else {
transitionContext.completeTransition(false)
return
}
// Animate manually
myRootVC.view.alpha = 0
// [...]
}
Do you know any way to capture this nicely?你知道有什么方法可以很好地捕捉到这一点吗?
I solved the similar problem by making all my ViewControllers
being presented as embedded ViewControllers using ViewController Containment.我通过使用 ViewController Containment 将我的所有ViewControllers
呈现为嵌入式 ViewControllers 解决了类似的问题。
I do have 1 RootContainerViewController
setup when launching the the application - it's simply set as rootViewController
without animations and it's just container for all other ViewControllers.启动应用程序时,我确实设置了 1 个RootContainerViewController
- 它只是设置为没有动画的rootViewController
,它只是所有其他 ViewController 的容器。
All other ViewControllers are always added as SubViewControllers and SubViews to that container.所有其他 ViewControllers 总是作为 SubViewControllers 和 SubViews 添加到该容器中。
That allows you not only to use UIViewControllerAnimatedTransitioning
for every single transition in your application, but also it allows you to easily make "reset" of the app, by just removing RootContainerViewController
and replacing it with fresh one.这不仅允许您对应用程序中的每个转换使用UIViewControllerAnimatedTransitioning
,而且还允许您轻松地“重置”应用程序,只需删除RootContainerViewController
并将其替换为新的。 Another advantage is that you can add as many ViewControllers as you want - compared to native presentViewController
that allows you to have only 1 ViewController presented at a time.另一个优点是您可以根据需要添加任意数量的 ViewController - 与本机presentViewController
相比,您一次只能显示 1 个 ViewController。
Any other approaches I tried ended up badly - UIViewControllerAnimatedTransitioning
is impossible to use directly with rootViewController
changes - you will have to combine it with UIView.animate
to support it.我尝试过的任何其他方法都以失败告终 - UIViewControllerAnimatedTransitioning
无法直接与rootViewController
更改一起使用 - 您必须将它与UIView.animate
结合起来才能支持它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.