You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.2 KiB

  1. //
  2. // SwipeNavigationController.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 06/09/2019.
  6. // Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class SwipeNavigationController: UINavigationController {
  10. // MARK: - Lifecycle
  11. override init(rootViewController: UIViewController) {
  12. super.init(rootViewController: rootViewController)
  13. delegate = self
  14. }
  15. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  16. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  17. delegate = self
  18. }
  19. required init?(coder aDecoder: NSCoder) {
  20. super.init(coder: aDecoder)
  21. delegate = self
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. // This needs to be in here, not in init
  26. interactivePopGestureRecognizer?.delegate = self
  27. }
  28. deinit {
  29. delegate = nil
  30. interactivePopGestureRecognizer?.delegate = nil
  31. }
  32. // MARK: - Overrides
  33. override func pushViewController(_ viewController: UIViewController, animated: Bool) {
  34. duringPushAnimation = true
  35. super.pushViewController(viewController, animated: animated)
  36. }
  37. // MARK: - Private Properties
  38. fileprivate var duringPushAnimation = false
  39. }
  40. // MARK: - UINavigationControllerDelegate
  41. extension SwipeNavigationController: UINavigationControllerDelegate {
  42. func navigationController(
  43. _ navigationController: UINavigationController,
  44. didShow viewController: UIViewController,
  45. animated: Bool
  46. ) {
  47. guard let swipeNavigationController = navigationController as? SwipeNavigationController else { return }
  48. swipeNavigationController.duringPushAnimation = false
  49. }
  50. }
  51. // MARK: - UIGestureRecognizerDelegate
  52. extension SwipeNavigationController: UIGestureRecognizerDelegate {
  53. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  54. guard gestureRecognizer == interactivePopGestureRecognizer else {
  55. return true // default value
  56. }
  57. // Disable pop gesture in two situations:
  58. // 1) when the pop animation is in progress
  59. // 2) when user swipes quickly a couple of times and animations don't have time to be performed
  60. return viewControllers.count > 1 && duringPushAnimation == false
  61. }
  62. }