在UIViewControllerAnimatedTransitioning委托中,transitionContext.fromViewController是nil

时间:2016-05-20 18:19:26

标签: ios swift uinavigationcontroller uiviewanimationtransition custom-transition

我有一个 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")
    }
}

Subclassed 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
    }
}

使用上述内容, fromVC toVC 均为零

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

1 个答案:

答案 0 :(得分:3)

您使用的是UITransitionContextFromViewKey,但您需要使用UITransitionContextFromViewControllerKey

同样为“to”