Ios 自定义UINavigationController动画

Ios 自定义UINavigationController动画,ios,swift,uiviewanimationtransition,ios-animations,Ios,Swift,Uiviewanimationtransition,Ios Animations,我正在尝试实现自定义过渡动画这里是我所拥有的 class NavDelegate: NSObject, UINavigationControllerDelegate { private let animator = Animator() func navigationController(_ navigationController: UINavigationController, animationControlle

我正在尝试实现自定义过渡动画这里是我所拥有的

class NavDelegate: NSObject, UINavigationControllerDelegate {
    private let animator = Animator()

    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationControllerOperation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return animator
    }
}

class Animator: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(using context: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 10.0
    }

    func animateTransition(using context: UIViewControllerContextTransitioning) {
        let fromViewController = context.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toViewController = context.viewController(forKey: UITransitionContextViewControllerKey.to)!
        if fromViewController is ViewController {
            self.pushAnimation(from: fromViewController as! ViewController,
                               to: toViewController as! VC2ViewController,
                               with: context)
        }

        if toViewController is ViewController {
            print("pop")
        }

    }

    func pushAnimation(from viewController: ViewController,
                       to destinationViewController: VC2ViewController,
                       with context: UIViewControllerContextTransitioning) {

        context.containerView.addSubview(destinationViewController.view)


        //block 1
        for cell in viewController.tableView1.visibleCells {
            if let castCell = cell as? VC1TableViewCell {
                castCell.contentViewToLeft.constant = -UIScreen.main.bounds.width
                castCell.contentViewToRight.constant = UIScreen.main.bounds.width
                let duration = Double(viewController.tableView1.visibleCells.index(of: cell)!)/Double(viewController.tableView1.visibleCells.count) + 0.2
                UIView.animate(withDuration: duration, animations: {
                    castCell.layoutIfNeeded()
                }) { animated in
                }
            }
        }

        //block 2
        for cell in destinationViewController.tableView2.visibleCells {
            if let castCell = cell as? VC2TableViewCell {
                castCell.contentViewToLeft.constant = -UIScreen.main.bounds.width
                castCell.contentViewToRight.constant = UIScreen.main.bounds.width
                let duration = Double(destinationViewController.tableView2.visibleCells.index(of: cell)!)/Double(destinationViewController.tableView2.visibleCells.count) + 0.2
                UIView.animate(withDuration: duration, animations: {
                    castCell.layoutIfNeeded()
                }) { animated in
                    if duration > 1.1 {
                        context.completeTransition(!context.transitionWasCancelled)
                    }
                }
            }
        }
    }
}

问题是,目标控制器(块2)约束的动画从未设置动画,
layoutifneed()
在没有动画的情况下执行,尽管块1正在工作。可能有什么问题?

我花了几天时间测试

transitionDuration(使用上下文:UIViewControllerContextTransitioning?)


函数,并发现这一切都是关于调用
DispatchQueue.main.async{}
中的
toViewController
动画的一些块。我不确定它是如何工作的,但我已经按照我的计划使我的代码正常工作。

我已经花了几天时间进行测试

transitionDuration(使用上下文:UIViewControllerContextTransitioning?)

函数,并发现这一切都是关于调用
DispatchQueue.main.async{}
中的
toViewController
动画的一些块。不知道它是如何工作的,但我已经按照我的计划使我的代码工作了