如何在 animationController (UIViewControllerTransitioningDelegate) 中使用源(不呈现)?
How to use source (not presenting) in animationController (UIViewControllerTransitioningDelegate)?
我正在制作自定义过渡管理器。它符合协议 UIViewControllerAnimatedTransitioning
& UIViewControllerTransitioningDelegate
.
当 present
从 RocketsVC
到 RocketDetailsVC
时,函数被调用 animationController(for Presented presented: UIViewController, presenting: UIViewController, source: UIViewController)
,下面的类型被传递到那里:
presented
: RocketDetailsViewController
presenting
: MainTabBarController
(错误在这里)
source
: RocketsViewController
此函数在 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
?
TransitionManager.swift
RocketsViewController.swift
MainTabBarController.swift
经过漫长的时间我找到的解决方案:
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
}
...
我正在制作自定义过渡管理器。它符合协议 UIViewControllerAnimatedTransitioning
& UIViewControllerTransitioningDelegate
.
当 present
从 RocketsVC
到 RocketDetailsVC
时,函数被调用 animationController(for Presented presented: UIViewController, presenting: UIViewController, source: UIViewController)
,下面的类型被传递到那里:
presented
:RocketDetailsViewController
presenting
:MainTabBarController
(错误在这里)source
:RocketsViewController
此函数在 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
?
TransitionManager.swift
RocketsViewController.swift
MainTabBarController.swift
经过漫长的时间我找到的解决方案:
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
}
...