JXNavigation.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //
  2. // JXNavigation.m
  3. // shiku_im
  4. //
  5. // Created by p on 2017/12/1.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXNavigation.h"
  9. #import "UIImage+Tint.h"
  10. @interface JXNavigation ()
  11. @property (nonatomic, strong) UIView *tableHeader;
  12. @property (nonatomic, assign) BOOL isReset;
  13. @end
  14. @implementation JXNavigation
  15. static JXNavigation *navigation;
  16. +(JXNavigation*)sharedInstance{
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. navigation=[[JXNavigation alloc]init];
  20. });
  21. return navigation;
  22. }
  23. -(instancetype)init{
  24. self = [super init];
  25. if(self){
  26. _navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT)];
  27. _navigationView.backgroundColor = [UIColor whiteColor];
  28. //此处添加到g_APP.window 添加到g_window 会导致push页面黑屏
  29. [g_App.window addSubview:_navigationView];
  30. _subViews = [NSMutableArray array];
  31. // APP从后台进入前台活跃状态
  32. [g_notify addObserver:self selector:@selector(applicationWillResignActive) name:kApplicationDidBecomeActive object:nil];
  33. }
  34. return self;
  35. }
  36. -(void)createHeaderView{
  37. if (self.tableHeader) {
  38. [self.tableHeader removeFromSuperview];
  39. self.tableHeader = nil;
  40. }
  41. self.tableHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_TOP)];
  42. UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_TOP)];
  43. if (THESIMPLESTYLE) {
  44. iv.image = [[UIImage imageNamed:@"navBarBackground"] imageWithTintColor:[UIColor whiteColor]];
  45. // [self LX_SetShadowPathWith:HEXCOLOR(0xD9D9D9) shadowOpacity:0.3 shadowRadius:2 shadowPathWidth:10 view:tableHeader];
  46. }else {
  47. iv.image = [g_theme themeTintImage:@"navBarBackground"];//[UIImage imageNamed:@"navBarBackground"];
  48. }
  49. iv.userInteractionEnabled = YES;
  50. [self.tableHeader addSubview:iv];
  51. JXLabel* p = [[JXLabel alloc]initWithFrame:CGRectMake(40, JX_SCREEN_TOP - 32, JX_SCREEN_WIDTH-40*2, 20)];
  52. p.backgroundColor = [UIColor clearColor];
  53. p.textAlignment = NSTextAlignmentCenter;
  54. p.textColor = [UIColor whiteColor];
  55. p.font = [UIFont systemFontOfSize:18.0];
  56. p.userInteractionEnabled = YES;
  57. p.didTouch = @selector(actionTitle:);
  58. p.delegate = self;
  59. p.changeAlpha = NO;
  60. [self.tableHeader addSubview:p];
  61. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(8, JX_SCREEN_TOP - 38, 31, 31)];
  62. [btn setBackgroundImage:[UIImage imageNamed:@"title_back_black_big"] forState:UIControlStateNormal];
  63. [btn addTarget:self action:@selector(actionQuit) forControlEvents:UIControlEventTouchUpInside];
  64. [self.tableHeader addSubview:btn];
  65. [self.navigationView addSubview:self.tableHeader];
  66. }
  67. - (void)actionQuit {
  68. UIViewController *vc = _subViews.lastObject;
  69. [self dismissViewController:vc animated:YES];
  70. }
  71. // APP进入活跃状态
  72. - (void)applicationWillResignActive {
  73. UIViewController *vc = self.subViews.lastObject;
  74. if (vc == self.lastVC) {
  75. return;
  76. }
  77. // 页面复位
  78. vc.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, vc.view.frame.size.height);
  79. self.lastVC.view.frame = CGRectMake(-JX_SCREEN_WIDTH, 0, self.lastVC.view.frame.size.width, self.lastVC.view.frame.size.height);
  80. [self.lastVC.view removeFromSuperview];
  81. vc.view.userInteractionEnabled = YES;
  82. self.lastVC.view.userInteractionEnabled = YES;
  83. }
  84. // 边缘手势
  85. - (void)screenEdgePanGestureRecognizer:(UIViewController *)viewController{
  86. UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];
  87. screenPan.edges = UIRectEdgeLeft;
  88. [viewController.view addGestureRecognizer:screenPan];
  89. if ([viewController isKindOfClass:[admobViewController class]]) {
  90. admobViewController *vc = (admobViewController *)viewController;
  91. [vc.tableBody.panGestureRecognizer requireGestureRecognizerToFail:screenPan];
  92. }else if([viewController isKindOfClass:[JXTableViewController class]]) {
  93. JXTableViewController *vc = (JXTableViewController *)viewController;
  94. [vc.tableView.panGestureRecognizer requireGestureRecognizerToFail:screenPan];
  95. }
  96. }
  97. //边缘手势事件
  98. -(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan
  99. {
  100. UIViewController *vc = self.subViews.lastObject;
  101. CGPoint p = [screenPan translationInView:vc.view]; // 移动点
  102. CGPoint velocity = [screenPan velocityInView:vc.view]; // 移动速度
  103. // 侧滑前将上一个view添加上
  104. [_navigationView addSubview:self.lastVC.view];
  105. // 侧滑时当前view与上一个view都不触发事件
  106. vc.view.userInteractionEnabled = NO;
  107. self.lastVC.view.userInteractionEnabled = NO;
  108. // 滑动时实时更改当前view与上一个view的frame
  109. vc.view.frame = CGRectMake(p.x, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  110. self.lastVC.view.frame = CGRectMake(p.x - JX_SCREEN_WIDTH, 0, self.lastVC.view.frame.size.width, self.lastVC.view.frame.size.height);
  111. self.isReset = YES;
  112. // 滑动结束
  113. if (screenPan.state == UIGestureRecognizerStateEnded) {
  114. // 已经结束手势,禁止重置Frame
  115. self.isReset = NO;
  116. // 滑动距离大于屏幕一半 或 滑动速度大于1000
  117. if (p.x > JX_SCREEN_WIDTH/2 || velocity.x > 1000) {
  118. [UIView animateWithDuration:0.3 animations:^{
  119. // 滑动结束更改当前view与上一个view的frame
  120. vc.view.frame = CGRectMake(JX_SCREEN_WIDTH, 0, JX_SCREEN_WIDTH, vc.view.frame.size.height);
  121. self.lastVC.view.frame = CGRectMake(0, 0, self.lastVC.view.frame.size.width, self.lastVC.view.frame.size.height);
  122. } completion:^(BOOL finished) {
  123. [g_server stopConnection:self];
  124. [_navigationView endEditing:YES];
  125. // 滑动结束时将当前view与上一个view接收触发事件
  126. vc.view.userInteractionEnabled = YES;
  127. self.lastVC.view.userInteractionEnabled = YES;
  128. vc.view.tag = 10000;
  129. // 如果是tableviewController 销毁header和footer
  130. if([vc isKindOfClass:[JXTableViewController class]]) {
  131. JXTableViewController *tableVC = (JXTableViewController *)vc;
  132. [tableVC actionQuit];
  133. [tableVC.footer removeFromSuperview];
  134. [tableVC.header removeFromSuperview];
  135. tableVC.header = nil;
  136. tableVC.footer = nil;
  137. }else if([vc isKindOfClass:[admobViewController class]]){
  138. admobViewController *admobVC = (admobViewController *)vc;
  139. [admobVC actionQuit];
  140. }
  141. // 滑动结束销毁当前view 并 重置上一个view lastVC
  142. if (self.subViews.count > 1) {
  143. [vc.view removeFromSuperview];
  144. [self.subViews removeObject:vc];
  145. if (self.subViews.count > 1) {
  146. self.lastVC = self.subViews[self.subViews.count - 2];
  147. }
  148. }
  149. }];
  150. }else {
  151. // 没滑动到一半时处理
  152. [UIView animateWithDuration:0.3 animations:^{
  153. vc.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, vc.view.frame.size.height);
  154. self.lastVC.view.frame = CGRectMake(-JX_SCREEN_WIDTH, 0, self.lastVC.view.frame.size.width, self.lastVC.view.frame.size.height);
  155. } completion:^(BOOL finished) {
  156. [self.lastVC.view removeFromSuperview];
  157. vc.view.userInteractionEnabled = YES;
  158. self.lastVC.view.userInteractionEnabled = YES;
  159. }];
  160. }
  161. }
  162. }
  163. // 入栈
  164. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
  165. // 添加入栈的view
  166. [_navigationView addSubview:viewController.view];
  167. // 取出上一个view
  168. UIViewController *vc = self.subViews.lastObject;
  169. vc.view.userInteractionEnabled = NO;
  170. if (animated) { // 有动画
  171. //添加viewController到屏幕右侧
  172. viewController.view.frame = CGRectMake(JX_SCREEN_WIDTH, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  173. //移动viewController到屏幕上并添加动画
  174. [UIView animateWithDuration:.3 animations:^{
  175. viewController.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  176. }];
  177. } else { // 没有动画
  178. // 直接设置viewController的位置
  179. viewController.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  180. }
  181. // 将当前控制器加入总栈
  182. [self.subViews addObject:viewController];
  183. if (self.subViews.count > 1 && animated) {
  184. // 更新lastVC
  185. self.lastVC = self.subViews[self.subViews.count - 2];
  186. // 创建侧滑手势
  187. [self screenEdgePanGestureRecognizer:viewController];
  188. }
  189. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  190. vc.view.userInteractionEnabled = YES;
  191. if (self.subViews.count > 1) {
  192. // 销毁上一个view
  193. [vc.view removeFromSuperview];
  194. }
  195. });
  196. }
  197. // 出栈
  198. - (void)dismissViewController:(UIViewController *)viewController animated:(BOOL)animated{
  199. UIViewController *vc = _subViews.lastObject;
  200. // 侧滑出栈已经处理过,,不需要下面的逻辑处理
  201. if (viewController.view.tag == 10000) {
  202. return;
  203. }
  204. // 如果销毁的view是最顶层的
  205. if (vc == viewController) {
  206. if (self.subViews.count >= 2) {
  207. vc.view.userInteractionEnabled = NO;
  208. // 取出上一个控制器
  209. UIViewController *lastVC = self.subViews[self.subViews.count - 2];
  210. [_navigationView addSubview:lastVC.view];
  211. if (animated) { //有动画
  212. // 先将上一个view添加到左侧屏幕外边
  213. lastVC.view.frame = CGRectMake(-JX_SCREEN_WIDTH, lastVC.view.frame.origin.y, lastVC.view.frame.size.width, lastVC.view.frame.size.height);
  214. [UIView animateWithDuration:0.3 animations:^{
  215. // 动画将上一个view移入屏幕
  216. lastVC.view.frame = CGRectMake(0, lastVC.view.frame.origin.y, lastVC.view.frame.size.width, lastVC.view.frame.size.height);
  217. }];
  218. } else { // 没有动画
  219. lastVC.view.frame = CGRectMake(0, lastVC.view.frame.origin.y, lastVC.view.frame.size.width, lastVC.view.frame.size.height);
  220. }
  221. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  222. vc.view.userInteractionEnabled = YES;
  223. if (self.subViews.count > 1) {
  224. // 延时将当前的要销毁的view销毁
  225. [vc.view removeFromSuperview];
  226. [self.subViews removeObject:vc];
  227. if (self.subViews.count > 1) {
  228. // 更新上一个控制器lastVC
  229. self.lastVC = self.subViews[self.subViews.count - 2];
  230. }
  231. }
  232. });
  233. }else {
  234. [self popToRootViewController];
  235. }
  236. }else {
  237. // 当销毁的view不是最上层的view,直接销毁
  238. [viewController.view removeFromSuperview];
  239. [self.subViews removeObject:viewController];
  240. if (self.subViews.count > 1) {
  241. self.lastVC = self.subViews[self.subViews.count - 2];
  242. }
  243. }
  244. }
  245. // 跳转至根视图
  246. - (void)popToRootViewController {
  247. //获取根视图并设置位置
  248. UIViewController *vc = self.subViews.firstObject;
  249. vc.view.frame = CGRectMake(0, vc.view.frame.origin.y, vc.view.frame.size.width, vc.view.frame.size.height);
  250. //必须添加到_navigationView,不添加会出现push页面出现在当前控制器后面的问题
  251. [_navigationView addSubview:vc.view];
  252. //移除所有视图
  253. for (NSInteger i = 1; i < self.subViews.count; i++) {
  254. UIViewController *vc = self.subViews[i];
  255. [vc.view removeFromSuperview];
  256. vc = nil;
  257. }
  258. //清空总栈
  259. [self.subViews removeAllObjects];
  260. //添加到总栈
  261. [self.subViews addObject:vc];
  262. //重置lastVC
  263. self.lastVC = nil;
  264. }
  265. // 设置根视图
  266. - (void)setRootViewController:(UIViewController *)rootViewController {
  267. [self createHeaderView];
  268. _rootViewController = rootViewController;
  269. //必须添加到_navigationView,不添加会出现push页面出现在当前控制器后面的问题
  270. [_navigationView addSubview:rootViewController.view];
  271. //设置rootViewController为根视图并显示
  272. g_App.window.rootViewController = rootViewController;
  273. [g_App.window makeKeyAndVisible];
  274. //移除除了根视图的所以视图
  275. for (NSInteger i = 0; i < self.subViews.count; i++) {
  276. UIViewController *vc = self.subViews[i];
  277. [vc.view removeFromSuperview];
  278. vc = nil;
  279. }
  280. //清空self.subViews
  281. [self.subViews removeAllObjects];
  282. //添加到总栈
  283. [self.subViews addObject:rootViewController];
  284. //重置lastVC
  285. self.lastVC = nil;
  286. }
  287. // 指定控制器回跳(只能跳转subViews中的VC)
  288. - (void)popToViewController:(Class)viewController animated:(BOOL)animated{
  289. UIViewController *vc;
  290. NSUInteger index;
  291. for (id object in self.subViews) {
  292. if ([object class] == viewController) {
  293. //获取当前控制器的坐标
  294. index = [self.subViews indexOfObject:object];
  295. //获取将要显示的控制器
  296. vc = self.subViews[index];
  297. }
  298. }
  299. if (!vc) return;
  300. [_navigationView addSubview:vc.view];
  301. if (animated) { // 有动画
  302. // 先将上一个view添加到左侧屏幕外边
  303. vc.view.frame = CGRectMake(-JX_SCREEN_WIDTH, vc.view.frame.origin.y, vc.view.frame.size.width, vc.view.frame.size.height);
  304. [UIView animateWithDuration:0.3 animations:^{
  305. // 动画将上一个view移入屏幕
  306. vc.view.frame = CGRectMake(0, vc.view.frame.origin.y, vc.view.frame.size.width, vc.view.frame.size.height);
  307. }];
  308. } else { // 没有动画
  309. vc.view.frame = CGRectMake(0, vc.view.frame.origin.y, vc.view.frame.size.width, vc.view.frame.size.height);
  310. }
  311. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  312. //销毁viewController后面的控制器
  313. for (NSUInteger i = index + 1; i < self.subViews.count; i++) {
  314. UIViewController *subVC = self.subViews[i];
  315. [subVC.view removeFromSuperview];
  316. }
  317. //从数组中移除多余的控制器
  318. [self.subViews removeObjectsInRange:NSMakeRange(index + 1, self.subViews.count - index - 1)];
  319. //更新上一个控制器
  320. if (self.subViews.count > 1) {
  321. self.lastVC = self.subViews[index - 1];
  322. } else {
  323. self.lastVC = nil;
  324. }
  325. });
  326. }
  327. - (void)resetVCFrame {
  328. UIViewController *vc = self.subViews.lastObject;
  329. if (vc == self.lastVC || !self.isReset) {
  330. return;
  331. }
  332. // 页面复位
  333. vc.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, vc.view.frame.size.height);
  334. self.lastVC.view.frame = CGRectMake(-JX_SCREEN_WIDTH, 0, self.lastVC.view.frame.size.width, self.lastVC.view.frame.size.height);
  335. [self.lastVC.view removeFromSuperview];
  336. vc.view.userInteractionEnabled = YES;
  337. self.lastVC.view.userInteractionEnabled = YES;
  338. }
  339. @end