ShortcutManager.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // ShortcutManager.m
  3. // VclustersGemini
  4. //
  5. // Created by 余衡武 on 2021/10/18.
  6. // Copyright © 2021 APPLE. All rights reserved.
  7. //
  8. #import "ShortcutManager.h"
  9. #import "AppDelegate.h"
  10. static ShortcutManager *shareInstance = nil;
  11. @interface ShortcutManager ()
  12. @end
  13. @implementation ShortcutManager
  14. + (instancetype)shareInstance {
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. shareInstance = [[ShortcutManager alloc] init];
  18. });
  19. return shareInstance;
  20. }
  21. #pragma mark 获取手机当前显示的ViewController
  22. //获取手机当前显示的ViewController 方法1-遍历法
  23. - (UIViewController *)getCurrentViewController {
  24. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  25. while (1)
  26. {
  27. if (vc.presentedViewController) {
  28. vc = vc.presentedViewController;
  29. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  30. vc = ((UITabBarController*)vc).selectedViewController;
  31. } else if ([vc isKindOfClass:[UINavigationController class]]) {
  32. vc = ((UINavigationController*)vc).visibleViewController;
  33. } else {
  34. break;
  35. }
  36. }
  37. return vc;
  38. }
  39. //获取手机当前显示的ViewController 方法1-递归法
  40. - (UIViewController *)findCurrentShowingViewController {
  41. //获得当前活动窗口的根视图
  42. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  43. UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc];
  44. return currentShowingVC;
  45. }
  46. - (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc
  47. {
  48. //方法1:递归方法 Recursive method
  49. UIViewController *currentShowingVC;
  50. if ([vc presentedViewController]) { //注要优先判断vc是否有弹出其他视图,如有则当前显示的视图肯定是在那上面
  51. // 当前视图是被presented出来的
  52. UIViewController *nextRootVC = [vc presentedViewController];
  53. currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
  54. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  55. // 根视图为UITabBarController
  56. UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];
  57. currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
  58. } else if ([vc isKindOfClass:[UINavigationController class]]){
  59. // 根视图为UINavigationController
  60. UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
  61. currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
  62. } else {
  63. // 根视图为非导航类
  64. currentShowingVC = vc;
  65. }
  66. return currentShowingVC;
  67. }
  68. // popToRootViewController
  69. - (void)popToRootViewController:(UIViewController *)vc {
  70. // 1、popToRootViewController
  71. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  72. if (vc.navigationController) {
  73. [vc.navigationController popToRootViewControllerAnimated:NO];
  74. }
  75. });
  76. // 2、tabBarController 选择第一个item (跟2不能同时执行 要做延时)
  77. vc.tabBarController.selectedIndex = 0;
  78. }
  79. // dismissToRootViewController
  80. - (void)dismissToRootViewController:(UIViewController *)vc {
  81. // 1、dismissToRootViewController
  82. while (vc.presentingViewController) {
  83. vc = vc.presentingViewController;
  84. }
  85. [vc dismissViewControllerAnimated:NO completion:^{
  86. UIViewController *vc1 = [self findCurrentShowingViewController];
  87. // 2、popToRootViewController
  88. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  89. if (vc1.navigationController) {
  90. [vc1.navigationController popToRootViewControllerAnimated:NO];
  91. }
  92. });
  93. // 3、tabBarController 选择第一个item (跟2不能同时执行 要做延时)
  94. vc1.tabBarController.selectedIndex = 0;
  95. }];
  96. }
  97. @end