iOS: Container view - animate push transition when changing child view controllers

ぃ、小莉子 提交于 2019-12-03 16:34:24

Load child view, set frame with origin.y under bottom screen. After change it to 0 in animation block. Example:

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()               
        })           
    }

Above code is transition between 2 child view with push and pop horizontal animation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!