iOS:容器视图-更改子视图控制器时动画推送过渡

Apo*_*oad 4 objective-c ios uicontainerview

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

奇怪的是,要在子视图控制器之间进行过渡,您不能调用类似的简单push方法UINavigationController来为过渡设置动画。

小智 5

加载子视图,并在底部屏幕下方使用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()               
        })           
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码是2个子视图之间的过渡,其中包含推和弹出水平动画。