我有一个UINavigationController设置,希望使用自定义动画来弹出/推送视图。我以前使用过自定义转换,但在本例中,我实际上在“from”和“to”UIViewControllers中找到了零值。
我的设置与此非常相似SO Post
自定义数据入口视图控制器
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
}
}
自定义BaseTrantiationAnimator
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")
}
}
子类传递animator
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怎么可能没有指向UIViewControllers的“to”和“from”的有效指针?
最佳答案
您正在使用UITransitionContextFromViewKey
,但需要使用UITransitionContextFromViewControllerKey
与“to”相同