如何在animationController(UIViewControllerTransitioningDelegate)中使用源(不呈现)?

发布于 2025-01-14 18:17:14 字数 2370 浏览 0 评论 0原文

我正在制作一个自定义过渡管理器。它符合协议 UIViewControllerAnimatedTransitioning & UIViewControllerTransitioningDelegate

presentingRocketsVCRocketDetailsVC时,func被称为animationController(for Presentedpresented: UIViewController,presenting:UIViewController,source:UIViewController ),以下类型被传递到那里:

  • presented: RocketDetailsViewController
  • presenting: MainTabBarController (错误在这里)
  • sourceRocketsViewController

这个函数在 TransitionManager.swift 中声明:

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    type = .presentation
    return self // TransitionManager
}

然后 animateTransition 方法被调用...

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromViewController = transitionContext.viewController(forKey: .from) as? TransitionManagerProtocol,
        let toViewController = transitionContext.viewController(forKey: .to) as? TransitionManagerProtocol

...效果为零,因为 MainTabBarController 不符合 TransitionManagerProtocol

如果我构建一个没有 MainTabBarController 的项目(rootVC 是 RocketsVC),那么一切都会按预期进行。

我应该怎么做才能使转换工作?我在 MainTabBarController 上犯了罪,但也许有一种方法可以以某种方式传递给 animationController 方法source 而不是 presenting

完整代码位于我的 GitHub

TransitionManager.swift
RocketsViewController.swift
MainTabBarController.swift

I'm making a custom Transition Manager. It conforms to the protocols UIViewControllerAnimatedTransitioning & UIViewControllerTransitioningDelegate.

When presenting from RocketsVC to RocketDetailsVC, func is called animationController(for Presented presented: UIViewController, presenting: UIViewController, source: UIViewController), the following types are passed there:

  • presented: RocketDetailsViewController
  • presenting: MainTabBarController (Error is here)
  • source: RocketsViewController

This functions are declared in TransitionManager.swift:

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    type = .presentation
    return self // TransitionManager
}

And then animateTransition method is called...

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromViewController = transitionContext.viewController(forKey: .from) as? TransitionManagerProtocol,
        let toViewController = transitionContext.viewController(forKey: .to) as? TransitionManagerProtocol

...with zero effect, because MainTabBarController does not conform to TransitionManagerProtocol.

If I build a project without MainTabBarController (rootVC is RocketsVC), then everything works as it should.

What should I do to make the transition work? I'm sinning on MainTabBarController, but maybe there is a way to somehow pass to animationController method source instead of presenting?

Full code is in my GitHub

TransitionManager.swift
RocketsViewController.swift
MainTabBarController.swift

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2025-01-21 18:17:14

我花了很长时间找到的解决方案:

private var fromVC: UIViewController?
private var toVC: UIViewController?

...

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    type = .presentation
    fromVC = source
    toVC = presented
    return self
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    type = .dismissal
    toVC = fromVC // last `fromVC` is now `toVC`
    fromVC = dismissed // and now we set this to fromVC
    return self
}

并且:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromViewController = fromVC as? TransitionManagerProtocol,
          let toViewController = toVC as? TransitionManagerProtocol
    else {
        transitionContext.completeTransition(false)
        return
    }
    ...

The solution I've found after long hours:

private var fromVC: UIViewController?
private var toVC: UIViewController?

...

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    type = .presentation
    fromVC = source
    toVC = presented
    return self
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    type = .dismissal
    toVC = fromVC // last `fromVC` is now `toVC`
    fromVC = dismissed // and now we set this to fromVC
    return self
}

And:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromViewController = fromVC as? TransitionManagerProtocol,
          let toViewController = toVC as? TransitionManagerProtocol
    else {
        transitionContext.completeTransition(false)
        return
    }
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文