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

时间:2017-02-08 19:04:14

标签: ios objective-c uicontainerview

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

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

1 个答案:

答案 0 :(得分:4)

加载子视图,在底部屏幕下使用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个子视图之间的过渡和弹出水平动画。