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() + } + +}