简体   繁体   English

iOS:容器视图 - 更改子视图控制器时的动画推送转换

[英]iOS: Container view - animate push transition when changing child view controllers

Apple discusses how to have a container view controller transition between two child view controllers in this document . Apple讨论了如何在本文档中的两个子视图控制器之间进行容器视图控制器转换。 I would like to animate a simple push vertical slide up identical to UIModalTransitionStyleCoverVertical in UIModalTransitionStyle . 我想在UIModalTransitionStyleCoverVerticalUIModalTransitionStyleUIModalTransitionStyleCoverVertical相同的简单垂直向上滑动。 However, transitionFromViewController only allows use of UIViewAnimationOptions , not transition styles. 但是, transitionFromViewController只允许使用UIViewAnimationOptions ,而不允许使用过渡样式。 So how would one animate sliding a view up? 那么如何动画滑动视图呢?

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. 奇怪的是,要在子视图控制器之间进行转换,您无法调用类似于UINavigationController的简单推送方法来为转换设置动画。

Load child view, set frame with origin.y under bottom screen. 加载子视图,在底部屏幕下使用origin.y设置框架。 After change it to 0 in animation block. 在动画块中将其更改为0后。 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()               
        })           
    }

Above code is transition between 2 child view with push and pop horizontal animation. 上面的代码是2个子视图之间的过渡和弹出水平动画。

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

相关问题 如何推送两个视图控制器但仅为第二个视图控制器设置动画过渡? - How to push two view controllers but animate transition only for the second one? 在添加到容器视图控制器/从容器视图控制器中删除时,如何为子视图控制器设置动画? - How do I animate child view controllers when adding to/removing from a container view controller? 动画过渡到子视图控制器 - animate transition to child view controller iOS 5之前的Container View Controller - Container View Controllers pre iOS 5 iOS:具有子视图控制器的自动布局 - iOS: Autolayout with child view controllers iOS-不同的Storyboard视图控制器之间的过渡 - iOS - Transition between different Storyboard view controllers 视图控制器之间的转换和iOS中的旋转 - Transition between view controllers and rotation in iOS 在iOS的容器视图内对图像视图进行动画处理? - Animate image view inside a container view in iOS? 无法为具有子视图控制器的两个容器UIView的位置变化制作动画 - Can't animate a position change of two container UIViews that have child view controllers 如何在 SWIFT 3 中使用按钮为 3 个或更多视图控制器之间的过渡设置动画? - How to animate a transition between 3 or more view controllers with buttons in SWIFT 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM