From e3751a52fecbfe6ce8a761c488dfabddbb56f8ad Mon Sep 17 00:00:00 2001 From: juanjovn Date: Sun, 1 Mar 2020 16:37:55 +0100 Subject: [PATCH] add capabilites for animating view transitions and fix a typo --- AMTabView/Classes/AMTabsViewController.swift | 29 +++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/AMTabView/Classes/AMTabsViewController.swift b/AMTabView/Classes/AMTabsViewController.swift index 4e566a9..a0bae69 100644 --- a/AMTabView/Classes/AMTabsViewController.swift +++ b/AMTabView/Classes/AMTabsViewController.swift @@ -151,14 +151,15 @@ open class AMTabsViewController: UIViewController { } let currentView = containerView.subviews.first - currentView?.removeFromSuperview() - let contoller = children[index] - let newView = contoller.view + let controller = children[index] + let newView = controller.view if newView?.superview == nil { containerView.addSubview(newView!) - contoller.didMove(toParent: self) + controller.didMove(toParent: self) newView?.frame = containerView.bounds + + doTransition(fromController: viewControllers![lastSelectedViewIndex ?? 0], toController: viewControllers![index], currentView: currentView, animate: true) } lastSelectedViewIndex = index @@ -186,3 +187,23 @@ extension AMTabsViewController: AMTabViewDelegate { } } + + +/// +/// Do the transition from current view to the target view. Allows to choose if the transition is animated or not. +/// +func doTransition(fromController: UIViewController?, toController: UIViewController?,currentView: UIView?, animate: Bool){ + + guard let fromView = fromController?.view, let toView = toController?.view else { + return + } + + if animate { + if fromView != toView { + UIView.transition(from: fromView, to: toView, duration: 0.3, options: [.transitionFlipFromLeft], completion: nil) + } + }else{ + currentView?.removeFromSuperview() + } + +}