iOS:容器视图 – 更改子视图控制器时的动画推送转换

Apple讨论了如何在本文档中的两个子视图控制器之间进行容器视图控制器转换。 我想动画一个简单的垂直向上滑动,与UIModalTransitionStyleCoverVertical中的UIModalTransitionStyle相同。 但是, transitionFromViewController只允许使用UIViewAnimationOptions ,而不允许使用过渡样式。 那么如何动画滑动视图呢?

奇怪的是,要在子视图控制器之间进行转换,您无法调用类似于UINavigationController的简单推送方法来为转换设置动画。

加载子视图,在bottom屏幕下设置origin.y框架。 在动画块中将其更改为0后。 例:

 enum Animation { case LeftToRight case RightToLeft } func animationForLoad(fromvc: UIViewController, tovc: UIViewController, with animation: Animation) { self.addChildViewController(tovc) self.container.addSubview(tovc.view) self.currentVC = tovc var endOriginx: CGFloat = 0 if animation == Animation.LeftToRight { tovc.view.frame.origin.x = -self.view.bounds.width endOriginx += fromvc.view.frame.width } else { tovc.view.frame.origin.x = self.view.bounds.width endOriginx -= fromvc.view.frame.width } self.transition(from: fromvc, to: tovc, duration: 0.35, options: UIViewAnimationOptions.beginFromCurrentState, animations: { tovc.view.frame = fromvc.view.frame fromvc.view.frame.origin.x = endOriginx }, completion: { (finish) in tovc.didMove(toParentViewController: self) fromvc.view.removeFromSuperview() fromvc.removeFromParentViewController() }) } 

上面的代码是2个子视图之间的过渡和弹出水平动画。