简体   繁体   English

动画过渡到子视图控制器

[英]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:您可以操作约束或使用CGAffineTransform ,如下所示:
mainView.containerView.transform = CGAffineTransform(translationX: UIScreen.main.bounds.size.width, y: 0)
  1. Call add(asChildViewController: viewController)调用add(asChildViewController: viewController)

  2. Move the container back to screen.将容器移回屏幕。 If you manipulated constraints in step 1, do it again.如果您在步骤 1 中操作了约束,请再做一次。 If you used CGAffineTransform , just write:如果您使用CGAffineTransform ,只需编写:

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.您可以在每次隐藏它时删除子视图控制器(如果它每次出现时都打算成为不同的视图控制器),或者您可以在viewDidLoad调用add(asChildViewController: viewController)一次,然后每次只重复步骤 1 和 3您需要显示和隐藏子视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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