GYSide.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // GYSide.swift
  3. // GYSide
  4. //
  5. // Created by gaoyuan on 2018/1/29.
  6. // Copyright © 2018年 gaoyuan. All rights reserved.
  7. //
  8. import UIKit
  9. var showControlelrTransitioningDelegateKey = "showControlelrTransitioningDelegateKey"
  10. let GYSideTapNotification = "GYSideTapNotification"
  11. let GYSidePanNotification = "GYSidePanNotification"
  12. let kScreenWidth = UIScreen.main.bounds.width
  13. extension UIViewController {
  14. /// 侧边栏出来
  15. ///
  16. /// - Parameters:
  17. /// - configuration: 配置
  18. /// - viewController: 将要展现的viewController
  19. public func gy_showSide(configuration:(GYSideConfig)->(), viewController:UIViewController) {
  20. let config = GYSideConfig()
  21. configuration(config)
  22. var delegate = objc_getAssociatedObject(self, &showControlelrTransitioningDelegateKey)
  23. if delegate == nil {
  24. delegate = GYSideTransitioningDelegate(config:config)
  25. objc_setAssociatedObject(viewController, &showControlelrTransitioningDelegateKey, delegate, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  26. }else {
  27. let d = delegate as! GYSideTransitioningDelegate
  28. d.config = config
  29. }
  30. let d = delegate as! GYSideTransitioningDelegate
  31. let dismissalInteractiveTransition = GYSidePercentInteractiveTransition(showType: .hidden, viewController:viewController, config: config)
  32. d.dismissalInteractiveTransition = dismissalInteractiveTransition
  33. viewController.transitioningDelegate = delegate as? UIViewControllerTransitioningDelegate
  34. DispatchQueue.main.async { //防止present延迟
  35. self.present(viewController, animated: true, completion: nil)
  36. }
  37. }
  38. /// 让侧边栏支持手势拖拽出来
  39. ///
  40. /// - Parameter completeShowGesture: 侧边栏展示的方向
  41. public func gy_registGestureShowSide(completeShowGesture:@escaping (GYSideDirection)->()) {
  42. let delegate = GYSideTransitioningDelegate(config: nil)
  43. let presentationInteractiveTransition = GYSidePercentInteractiveTransition(showType: .show, viewController: nil, config: nil)
  44. presentationInteractiveTransition.addPanGesture(fromViewController: self)
  45. presentationInteractiveTransition.completeShowGesture = completeShowGesture
  46. delegate.presentationInteractiveTransition = presentationInteractiveTransition
  47. self.transitioningDelegate = delegate
  48. objc_setAssociatedObject(self, &showControlelrTransitioningDelegateKey, delegate, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  49. }
  50. public func gy_sidePushViewController(viewController: UIViewController) {
  51. let rootVC: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
  52. var nav: UINavigationController?
  53. if rootVC.isKind(of: UITabBarController.classForCoder()) {
  54. let tabBar: UITabBarController = rootVC as! UITabBarController
  55. nav = tabBar.selectedViewController as? UINavigationController
  56. }else if rootVC.isKind(of: UINavigationController.classForCoder()) {
  57. nav = rootVC as? UINavigationController
  58. }else {
  59. fatalError("没有UINavigationController")
  60. }
  61. self.dismiss(animated: true, completion: nil)
  62. nav?.pushViewController(viewController, animated: false)
  63. }
  64. /// 侧边栏调用present
  65. ///
  66. /// - Parameter viewController
  67. public func gy_sidePresentViewController(viewController: UIViewController) {
  68. let rootVC: UIViewController = (UIApplication.shared.keyWindow?.rootViewController)!
  69. if ((rootVC.presentedViewController) != nil) {
  70. rootVC.presentedViewController?.dismiss(animated: true, completion: {
  71. DispatchQueue.main.async {
  72. rootVC.present(viewController, animated: true, completion: nil)
  73. }
  74. })
  75. }
  76. }
  77. }