ios - 动画过渡到 subview Controller

标签 ios swift

example booking

如何为 subview Controller 的过渡设置动画,如 Booking 动画,您可以在上面看到的示例。

我的代码:

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

我试试:

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

但是找不到合适的动画

enter image description here

最佳答案

为什么不尝试以下方法:

  1. 转换容器 View ,使其在屏幕之外。您可以操纵约束或使用 CGAffineTransform,如下所示:
mainView.containerView.transform = CGAffineTransform(translationX: UIScreen.main.bounds.size.width, y: 0)
  1. 调用add(asChildViewController: viewController)

  2. 将容器移回屏幕。如果您在步骤 1 中操纵了约束,请再做一次。如果你使用了CGAffineTransform,只需写:

mainView.containerView.transform = .identity

当你想要动画转换时,只需像这样包装它:

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

确保您没有在彼此之上添加多个 subview Controller 。您可以在每次隐藏它时删除 subview Controller (如果它每次出现时都打算成为不同的 View Controller ),或者您可以在 viewDidLoad 中调用一次 add(asChildViewController: viewController) 然后每次需要显示和隐藏 subview Controller 时只重复步骤 1 和 3。

关于ios - 动画过渡到 subview Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59769268/

相关文章:

ios - 使用 AppDelegate?

ios - Realm 的 Swift 3.0 编译的 Swift 模块在 Swift 3.0.1 无法导入

ios - 将第一个字母设为粗体 - ios swift

ios - FFMPEG 创建模糊视频

ios - Facebook 图形 API 链接参数返回 nil

ios - 使用 relativeToURL 初始值设定项快速发布构建 URL

ios - 为什么我得到 "[__NSArrayI allKeys]: unrecognized selector sent to instance"/为什么 NSDictionary 正在转换?

iOS - 测量距离的最佳方式

ios - 带有静态表的动态类型和自调整单元格

ios - 如何以编程方式检测用户何时在“今日 View ”中启用小部件