在 UIViewControllerAnimatedTransitioning 委托中,transitionContext.fromViewController 为 nil

Inside UIViewControllerAnimatedTransitioning delegate, the transitionContext.fromViewController is nil

我有一个 UINavigationController 设置,我希望使用自定义动画来弹出/推送视图。我之前使用过自定义转换没有问题,但在这种情况下,我实际上在我的 'from' 和 'to' UIViewControllers.

中找到了 nil 值

我的设置与此非常相似SO Post

自定义 DataEntryViewController

class DataEntryViewController : UIViewController, DataEntryViewDelegate, UINavigationControllerDelegate {

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {        
        let animator = DataEntryTransitionAnimator()
        animator.duration = 2
        return animator
    }
}

自定义 BaseTransitionAnimator

class BaseTransitionAnimator : NSObject, UIViewControllerAnimatedTransitioning {

    var duration : NSTimeInterval = 0.5             // default transition time of 1/2 second
    var appearing : Bool = true                     // is the animation appearing (or disappearing)

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return duration
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        assert(true, "animateTransition MUST be implemented by child class")
    }
}

子类化的 TransitionAnimator

class DataEntryTransitionAnimator : BaseTransitionAnimator {

    override func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey) as! DataEntryViewController
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewKey) as! DataEntryViewController
        let duration = transitionDuration(transitionContext)

        // do fancy animations
    }
}

使用上面的方法,fromVCtoVC 都是 nil

transitionContext 怎么可能没有指向 'to' 和 'from' UIViewControllers[=35 的有效指针=]?

您正在使用 UITransitionContextFromViewKey 但您需要使用 UITransitionContextFromViewControllerKey

"to"

相同