iOS:容器视图 - 更改子视图控制器时的动画推送转换 [英] iOS: Container view - animate push transition when changing child view controllers
问题描述
Apple讨论了如何在两个子视图控制器之间进行容器视图控制器转换文档。我想在 UIModalTransitionStyle
中设置与 UIModalTransitionStyleCoverVertical
相同的简单推送垂直滑动。但是, transitionFromViewController
只允许使用 UIViewAnimationOptions
,而不是转换样式。那么如何动画播放视图呢?
Apple discusses how to have a container view controller transition between two child view controllers in this document. I would like to animate a simple push vertical slide up identical to UIModalTransitionStyleCoverVertical
in UIModalTransitionStyle
. However, transitionFromViewController
only allows use of UIViewAnimationOptions
, not transition styles. So how would one animate sliding a view up?
奇怪的是,在子视图控制器之间进行转换,你不能在 UINavigationController
为转换设置动画。
It's odd that to transition between child view controllers you can't call a simple push method similar in UINavigationController
to animate the transition.
推荐答案
加载子视图,在origin.y下设置框架底部屏幕。在动画块中将其更改为0后。示例:
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()
})
}
上面的代码是2个子视图之间的过渡和弹出水平动画。
Above code is transition between 2 child view with push and pop horizontal animation.
这篇关于iOS:容器视图 - 更改子视图控制器时的动画推送转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!