所以,我在我的animateTransition func里有这段代码,如果我试图借助包含这个func的transitionManager来展示一个viewcontroller,它就找不到from view。
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// Getting the reference to container, toView & fromView
let container = transitionContext.containerView
let fromView = transitionContext.view(forKey: .from)! // Force unpacked
let toView = transitionContext.view(forKey: .to)! // Force unpacked
// Setup for the 2d animation
let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
let offScreenLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)
// Start the toView to the right of the screen
container.addSubview(toView)
container.addSubview(fromView)
// Get the duration of the animation
let duration = self.transitionDuration(using: transitionContext)
// Perform the animation
UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, animations: {
fromView.transform = offScreenLeft
toView.transform = .identity
}, completion: { finished in
// Tell our transitionContext object that we've finished animating
transitionContext.completeTransition(true)
})
}
如果我试图用TransitionManager的帮助来展示一个viewcontroller, 它包含了这个func, 它找不到from view. 如果我想解散,它找不到to view。我怎样才能告诉它从现在的哪里来,怎样才能告诉它在解散时要去哪里?
好了,我找到了解决方案。我之所以不能根据 dismiss 或 present 来获取 fromView 或 toView 的引用,是因为我调用了:UIViewControllerContextTransitioning。
let fromView = transitionContext.view(forKey: .from)!
let toView = transitionContext.view(forKey: .to)!
我应该调用:
let fromView = transitionContext.viewController(forkey: .from)!.view!
let toView = transitionContext.viewController(forkey: .to)!.view!