如何为 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)
但是找不到合适的动画
最佳答案
为什么不尝试以下方法:
- 转换容器 View ,使其在屏幕之外。您可以操纵约束或使用
CGAffineTransform
,如下所示:
mainView.containerView.transform = CGAffineTransform(translationX: UIScreen.main.bounds.size.width, y: 0)
调用
add(asChildViewController: viewController)
将容器移回屏幕。如果您在步骤 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 中调用一次
然后每次需要显示和隐藏 subview Controller 时只重复步骤 1 和 3。add(asChildViewController: viewController)
关于ios - 动画过渡到 subview Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59769268/