以编程方式让我的过渡经理知道我来自哪里或要去哪里? Swift

Programmatically letting my transition manager know where im coming from or where im going? Swift

所以我的 animateTransition 函数中有这段代码:

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
      // Getting the reference to container, toView & fromView
      let container = transitionContext.containerView
      let fromView = transitionContext.view(forKey: .from)! // Force unpacked
      let toView = transitionContext.view(forKey: .to)! // Force unpacked

      // Setup for the 2d animation
      let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
      let offScreenLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)

      // Start the toView to the right of the screen
      container.addSubview(toView)
      container.addSubview(fromView)

      // Get the duration of the animation
      let duration = self.transitionDuration(using: transitionContext)

      // Perform the animation
      UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, animations: {

        fromView.transform = offScreenLeft
        toView.transform = .identity

      }, completion: { finished in

        // Tell our transitionContext object that we've finished animating
        transitionContext.completeTransition(true)
      })

    }

如果我试图在包含此功能的 transitionManager 的帮助下呈现一个 viewcontroller,它无法找到从视图。如果我试图解雇,它找不到要查看的内容。我怎么能告诉它它现在从哪里来,我怎么能告诉它解雇时它必须去哪里?

好的,所以我找到了解决方案。我无法根据 dismiss 或 present 到达 fromView 或 toView 的原因是因为我调用了:

let fromView = transitionContext.view(forKey: .from)! 
let toView = transitionContext.view(forKey: .to)!

相反,我应该打电话给:

let fromView = transitionContext.viewController(forkey: .from)!.view!
let toView = transitionContext.viewController(forkey: .to)!.view!