简体   繁体   English

动画根视图 controller 嵌入到导航 controller 以模态方式呈现

[英]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.

相关问题 从模态呈现的视图控制器导航到根视图控制器 - Navigate to root view-controller from modally presented view controller 导航 Controller segue 模态呈现 - Navigation Controller segue is presented modally 如何检查视图控制器是模态呈现还是推送到导航堆栈上? - How to check if a view controller is presented modally or pushed on a navigation stack? 如何以模态方式呈现嵌入导航控制器中的视图控制器? - How to present view controller embedded in navigation controller modally? 导航控制器的“后退按钮”文本:以模态显示 - Navigation Controller “back button” text: Presented Modally iOS取消了模态呈现的导航控制器 - iOS dismissing a modally presented Navigation Controller 横向模式下的导航控制器堆栈,但模式呈现的视图控制器视图始终为纵向帧大小 - Navigation controller stack in landscape mode, but modally presented view controller view always in portrait frame size 呈现视图中的导航控制器 - Navigation Controller in presented view 在以模态表示的View Controller上调用viewDidLoad - Calling viewDidLoad on a View Controller presented Modally willRotateToInterfaceOrientation未在模态呈现的视图控制器中调用 - willRotateToInterfaceOrientation not being called in a modally presented view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM