iOS:容器视图 - 更改子视图控制器时动画推送过渡
iOS: Container view - animate push transition when changing child view controllers
Apple 在此 document 中讨论了如何在两个子视图控制器之间进行容器视图控制器转换。我想为一个简单的垂直向上滑动设置动画,与 UIModalTransitionStyle
中的 UIModalTransitionStyleCoverVertical
相同。但是,transitionFromViewController
只允许使用 UIViewAnimationOptions
,而不是过渡样式。那么如何制作向上滑动视图的动画呢?
奇怪的是,要在子视图控制器之间进行转换,您不能调用类似于 UINavigationController
的简单推送方法来设置转换动画。
加载子视图,在底部屏幕下设置框架origin.y。在动画块中将其更改为 0 后。示例:
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 个子视图之间的转换,带有推送和弹出水平动画。
Apple 在此 document 中讨论了如何在两个子视图控制器之间进行容器视图控制器转换。我想为一个简单的垂直向上滑动设置动画,与 UIModalTransitionStyle
中的 UIModalTransitionStyleCoverVertical
相同。但是,transitionFromViewController
只允许使用 UIViewAnimationOptions
,而不是过渡样式。那么如何制作向上滑动视图的动画呢?
奇怪的是,要在子视图控制器之间进行转换,您不能调用类似于 UINavigationController
的简单推送方法来设置转换动画。
加载子视图,在底部屏幕下设置框架origin.y。在动画块中将其更改为 0 后。示例:
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 个子视图之间的转换,带有推送和弹出水平动画。