简体   繁体   中英

animate transition to child view controller

example booking

How can I animate transition to child view controller, as Booking animate, example you can see above.

my code :

private func add(asChildViewController viewController: UIViewController) {

    addChild(viewController)
    mainView.containerView.addSubview(viewController.view)

    viewController.view.frame = mainView.containerView.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    viewController.didMove(toParent: self)
}

I try :

UIView.transition(with: self.view, duration: 0.5, options: .transitionFlipFromRight, animations: {
        viewController.didMove(toParent: self)
    }, completion: nil)

but I can't find the right animation

在此处输入图片说明

Why don't you try the following:

  1. Transform the container view, so that it is outside the screen. You can either manipulate constraints or use CGAffineTransform , like this:
mainView.containerView.transform = CGAffineTransform(translationX: UIScreen.main.bounds.size.width, y: 0)
  1. Call add(asChildViewController: viewController)

  2. Move the container back to screen. If you manipulated constraints in step 1, do it again. If you used CGAffineTransform , just write:

mainView.containerView.transform = .identity

When you want to animate the transition, just wrap it like this:

UIView.animate(
    withDuration: duration,
    delay: 0,
    options: .curveEaseInOut,
    animations: {
        mainView.containerView.transform = .identity
    }
)

Make sure that you do not add multiple child view controller on top of each other. You can either remove child view controller every time you hide it (if it intended to be a different view controller each time it appears) or you can call add(asChildViewController: viewController) once in viewDidLoad and then only repeat steps 1 and 3 every time you need to show and hide the child view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM