CCMainTabBarController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. fromView.superview?.backgroundColor = UIColor.white
  50. // Position toView off screen (to the left/right of fromView)
  51. let screenWidth = UIScreen.main.bounds.size.width;
  52. let scrollRight = toIndex > fromIndex!;
  53. let offset = (scrollRight ? screenWidth : -screenWidth)
  54. toView.center = CGPoint(x: (fromView.center.x) + offset, y: (toView.center.y))
  55. // Disable interaction during animation
  56. view.isUserInteractionEnabled = false
  57. UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
  58. // Slide the views by -offset
  59. fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y);
  60. toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y);
  61. }, completion: { finished in
  62. // Remove the old view from the tabbar view.
  63. fromView.removeFromSuperview()
  64. self.selectedIndex = toIndex
  65. self.view.isUserInteractionEnabled = true
  66. })
  67. }
  68. }