简体   繁体   English

在添加到容器视图控制器/从容器视图控制器中删除时,如何为子视图控制器设置动画?

[英]How do I animate child view controllers when adding to/removing from a container view controller?

I have the following 2 functions that add and remove child view controllers triggered from a container view controller: 我有以下2个函数,用于添加和删除从容器视图控制器触发的子视图控制器:

@discardableResult func addChildViewController(withChildViewController childViewController: UIViewController) -> UIViewController {
    // Add Child View Controller
    addChildViewController(childViewController)
    childViewController.beginAppearanceTransition(true, animated: true)
    // Add Child View as Subview
    view.addSubview(childViewController.view)
    // Configure Child View
    childViewController.view.frame = view.bounds
    childViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    // Notify Child View Controller
    childViewController.didMove(toParentViewController: self)
    return childViewController
}
@discardableResult func removeChildViewController(withChildViewController childViewController: UIViewController) -> UIViewController {
    // Notify Child View Controller
    childViewController.willMove(toParentViewController: nil)
    childViewController.beginAppearanceTransition(false, animated: true)
    // Remove Child View From Superview
    childViewController.view.removeFromSuperview()
    // Notify Child View Controller
    childViewController.removeFromParentViewController()
    return childViewController
}

The functions above are extensions to UIViewController, so all I'm doing is self.addChildViewController() and self.removeChildViewController() on the parent view controller. 上面的函数是UIViewController的扩展,所以我要做的就是父视图控制器上的self.addChildViewController()和self.removeChildViewController()。

How do I animate the view being removed on its way out and the view being added on its way in? 我该如何为在离开视图时删除的视图和在进入视图时添加的视图设置动画?

Animating between different child view controllers:- 在不同的子视图控制器之间进行动画处理:-

func cycleFromViewController(oldViewController: UIViewController, toViewController newViewController: UIViewController) {
    oldViewController.willMove(toParentViewController: nil)
    newViewController.view.translatesAutoresizingMaskIntoConstraints = false

    self.addChildViewController(newViewController)
    self.addSubview(subView: newViewController.view, toView:self.containerView!)

    newViewController.view.alpha = 0
    newViewController.view.layoutIfNeeded()

    UIView.animate(withDuration: 0.5, delay: 0.1, options: .transitionFlipFromLeft, animations: { 
        newViewController.view.alpha = 1
        oldViewController.view.alpha = 0
    }) { (finished) in
        oldViewController.view.removeFromSuperview()
        oldViewController.removeFromParentViewController()
        newViewController.didMove(toParentViewController: self)
    }
}

In above, 在上面,

  • oldViewController:- Current displayed child viewController oldViewController:-当前显示的子viewController
  • newViewController:- New child view controller that will going to add newViewController:-将要添加的新子视图控制器
  • containerView:- A view in which all child controllers are displaying. containerView:-显示所有子控制器的视图。

To animate child view, you can use different type of animation style by replacing transitionFlipFromLeft to available UIViewAnimationOptions according requirement. 若要设置子视图的动画,可以根据需要通过将transitionFlipFromLeft替换为可用的UIViewAnimationOptions来使用不同类型的动画样式。

Answer of Sagar is not complete addSubview method is missing there Sagar的答案不完整,那里缺少addSubview方法

Here is full answer 这是完整答案

func cycleFromViewController(oldViewController: UIViewController, toViewController newViewController: UIViewController) {
        oldViewController.willMove(toParentViewController: nil)
        newViewController.view.translatesAutoresizingMaskIntoConstraints = false

        self.addChildViewController(newViewController)
        self.addSubview(subView: newViewController.view, toView:self.containerView!)

        newViewController.view.alpha = 0
        newViewController.view.layoutIfNeeded()

        UIView.animate(withDuration: 0.5, delay: 0.1, options: .transitionFlipFromLeft, animations: {
            newViewController.view.alpha = 1
            oldViewController.view.alpha = 0
        }) { (finished) in
            oldViewController.view.removeFromSuperview()
            oldViewController.removeFromParentViewController()
            newViewController.didMove(toParentViewController: self)
        }
    }

    //--------------------------------------------------------------------------------


   private func addSubview(subView:UIView, toView parentView:UIView) {
        self.view.layoutIfNeeded()
        parentView.addSubview(subView)

        subView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor).isActive = true
        subView.topAnchor.constraint(equalTo: parentView.topAnchor).isActive = true
        subView.bottomAnchor.constraint(equalTo: parentView.bottomAnchor).isActive = true
        subView.trailingAnchor.constraint(equalTo: parentView.trailingAnchor).isActive  = true
    }

Hope it is helpful to someone 希望对别人有帮助

暂无
暂无

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

相关问题 如何使用无限子控制器构建视图控制器容器? - How to build a view controller container with infinite child controllers? 如何从未连接到其他视图控制器的视图控制器传输回视图控制器? - How do I transfer back to a view controller from a view controller that is not connected to other view controllers? iOS:容器视图 - 更改子视图控制器时的动画推送转换 - iOS: Container view - animate push transition when changing child view controllers 切换标签时,删除所有子视图控制器并移至父视图控制器IOS目标c - When switching tabs removing All Child View Controllers and Move to Parent View Controller IOS Objective c 如何从嵌入在父视图控制器的子视图控制器中的 UISearchViewController 呈现视图控制器? - How do I present a view controller from UISearchViewController embedded in a child view controller of a parent view controller? 如何将3个视图控制器嵌入1个视图控制器中? - How do I embed 3 view controllers into 1 view controller? 从父视图控制器中删除子视图控制器时应用程序崩溃 - App crashes when removing child view controller from its parent 在表格视图 controller 添加容器视图时,如何将其移动到最底部? (迅速) - In a table view controller when adding a container view, how can I move it to the very bottom? (Swift) 处理容器控制器中子视图控制器 UINavigationItem 更改的最佳实践? - Best practices for handling changes to the UINavigationItem of child view controllers in a container controller? 如何从嵌入式容器视图的视图引用视图控制器的标题? - How do I refer to the title of a view controller from an embedded container view's view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM