CCMainTabBarController.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // CCMainTabBarController.swift
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/03/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class CCMainTabBarController : UITabBarController, UITabBarControllerDelegate {
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. delegate = self
  28. }
  29. //Delegate methods
  30. func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  31. let tabViewControllers = tabBarController.viewControllers!
  32. guard let toIndex = tabViewControllers.index(of: viewController) else {
  33. if let vc = viewController as? UINavigationController {
  34. vc.popToRootViewController(animated: true);
  35. }
  36. return false
  37. }
  38. animateToTab(toIndex: toIndex)
  39. return true
  40. }
  41. func animateToTab(toIndex: Int) {
  42. let tabViewControllers = viewControllers!
  43. let fromView = selectedViewController!.view!
  44. let toView = tabViewControllers[toIndex].view!
  45. let fromIndex = tabViewControllers.index(of: selectedViewController!)
  46. guard fromIndex != toIndex else {return}
  47. // Add the toView to the tab bar view
  48. fromView.superview?.addSubview(toView)
  49. // Position toView off screen (to the left/right of fromView)
  50. let screenWidth = UIScreen.main.bounds.size.width;
  51. let scrollRight = toIndex > fromIndex!;
  52. let offset = (scrollRight ? screenWidth : -screenWidth)
  53. toView.center = CGPoint(x: (fromView.center.x) + offset, y: (toView.center.y))
  54. // Disable interaction during animation
  55. view.isUserInteractionEnabled = false
  56. UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
  57. // Slide the views by -offset
  58. fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y);
  59. toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y);
  60. }, completion: { finished in
  61. // Remove the old view from the tabbar view.
  62. fromView.removeFromSuperview()
  63. self.selectedIndex = toIndex
  64. self.view.isUserInteractionEnabled = true
  65. })
  66. }
  67. }