InventoryFreezeHomeViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // InventoryFreezeHomeViewController.m
  3. // IBOSS
  4. //
  5. // Created by 关宏厚 on 2020/4/28.
  6. // Copyright © 2020 elongtian. All rights reserved.
  7. //
  8. #import "InventoryFreezeHomeViewController.h"
  9. @interface InventoryFreezeHomeViewController ()
  10. @end
  11. /**
  12. 导航数
  13. */
  14. static int navcount = 2;
  15. @implementation InventoryFreezeHomeViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. [self loadNavStyle];
  19. [self initUI];
  20. [self setMainSrollView];
  21. [self sliderWithTag:self.currentIndex+1];
  22. }
  23. /**
  24. 导航按钮样式
  25. */
  26. - (void)loadNavStyle
  27. {
  28. self.navigationItem.title = @"库存冻结";
  29. //返回
  30. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  31. [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
  32. [button addTarget:self action:@selector(goBack)
  33. forControlEvents:UIControlEventTouchUpInside];
  34. button.frame = CGRectMake(0, 0,45,22);
  35. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  36. self.navigationItem.leftBarButtonItem = menuButton;
  37. }
  38. /**
  39. 安全区视图发生变化
  40. */
  41. -(void)viewSafeAreaInsetsDidChange{
  42. self.view.backgroundColor = [UIColor whiteColor];
  43. _mainScrollView.frame = CGRectMake(0, 40, Screen_Width,self.view.safeAreaLayoutGuide.layoutFrame.size.height-40);
  44. for (NSInteger i = 0; i < _mainScrollView.subviews.count; i++) {
  45. _mainScrollView.subviews[i].frame = CGRectMake(Screen_Width * i, 0, Screen_Width,CGRectGetHeight(_mainScrollView.frame));
  46. }
  47. [self.goodsSearchVC viewSafeAreaInsetsDidChange];
  48. [self.orderSearchVC viewSafeAreaInsetsDidChange];
  49. [super viewSafeAreaInsetsDidChange];
  50. }
  51. /**
  52. * 返回函数
  53. */
  54. - (void)goBack
  55. {
  56. [self.navigationController popViewControllerAnimated:YES];
  57. }
  58. /**
  59. 产品搜索vc
  60. @return <#return value description#>
  61. */
  62. - (InventoryFreezeGoodsSearchViewController *)goodsSearchVC{
  63. if (_goodsSearchVC == nil) {
  64. _goodsSearchVC = [[InventoryFreezeGoodsSearchViewController alloc] init];
  65. _goodsSearchVC.cNav=self.navigationController;
  66. }
  67. return _goodsSearchVC;
  68. }
  69. /**
  70. 历史订单vc
  71. @return
  72. */
  73. - (InventoryFrozenOrderViewController *)orderSearchVC{
  74. if (_orderSearchVC == nil) {
  75. _orderSearchVC= [[InventoryFrozenOrderViewController alloc] init];
  76. _orderSearchVC.cNav=self.navigationController;
  77. }
  78. return _orderSearchVC;
  79. }
  80. /*
  81. 初始化UI
  82. */
  83. - (void)initUI
  84. {
  85. UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
  86. [self.view addSubview:navView];
  87. [self.view setBackgroundColor:[UIColor whiteColor]];
  88. _productSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
  89. [_productSearchButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  90. [_productSearchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  91. _productSearchButton.frame = CGRectMake(0, 0, Screen_Width/navcount, navView.frame.size.height);
  92. _productSearchButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  93. [_productSearchButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  94. [_productSearchButton setTitle:@"商品查询" forState:UIControlStateNormal];
  95. _productSearchButton.tag = 1;
  96. _productSearchButton.selected = YES;
  97. [navView addSubview:_productSearchButton];
  98. _frozenOrderButton= [UIButton buttonWithType:UIButtonTypeCustom];
  99. _frozenOrderButton.frame = CGRectMake(CGRectGetMaxX(_productSearchButton.frame),0, Screen_Width/navcount, navView.frame.size.height);
  100. [_frozenOrderButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  101. [_frozenOrderButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  102. _frozenOrderButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  103. [_frozenOrderButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  104. [_frozenOrderButton setTitle:@"冻结订单" forState:UIControlStateNormal];
  105. _frozenOrderButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  106. _frozenOrderButton.tag = 2;
  107. [navView addSubview: _frozenOrderButton];
  108. _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(_productSearchButton.frame.origin.x, 40-2, Screen_Width/navcount, 2)];
  109. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(((Screen_Width/navcount)-70)/2, 0, 70, 2)];
  110. line.backgroundColor = [UIColor redColor];
  111. [_sliderLabel addSubview:line];
  112. [navView addSubview:_sliderLabel];
  113. }
  114. /**
  115. 按钮点击事件
  116. @param sender <#sender description#>
  117. */
  118. - (void)sliderAction:(UIButton *)sender{
  119. if (self.currentIndex == sender.tag) {
  120. return;
  121. }
  122. [self.view endEditing:YES];
  123. [self sliderAnimationWithTag:sender.tag];
  124. [UIView animateWithDuration:0.3 animations:^{
  125. self->_mainScrollView.contentOffset = CGPointMake(Screen_Width*(sender.tag-1), 0);
  126. } completion:^(BOOL finished) {
  127. }];
  128. }
  129. /**
  130. 滑动tag
  131. @param tag <#tag description#>
  132. */
  133. - (void)sliderAnimationWithTag:(NSInteger)tag{
  134. self.currentIndex = tag;
  135. _productSearchButton.selected = NO;
  136. _frozenOrderButton.selected = NO;
  137. UIButton *sender = [self buttonWithTag:tag];
  138. sender.selected = YES;
  139. //动画
  140. [UIView animateWithDuration:0.3 animations:^{
  141. self->_sliderLabel.frame = CGRectMake(sender.frame.origin.x, self->_sliderLabel.frame.origin.y, self->_sliderLabel.frame.size.width, self->_sliderLabel.frame.size.height);
  142. } completion:^(BOOL finished) {
  143. }];
  144. }
  145. /*
  146. 获得导航按钮
  147. */
  148. - (UIButton *)buttonWithTag:(NSInteger )tag{
  149. if (tag == 1) {
  150. return _productSearchButton;
  151. }
  152. else if (tag == 2){
  153. return _frozenOrderButton;
  154. }
  155. else{
  156. return nil;
  157. }
  158. }
  159. /**
  160. 初始化srollView
  161. */
  162. - (void)setMainSrollView{
  163. _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, Screen_Width, self.view.frame.size.height)];
  164. _mainScrollView.delegate = self;
  165. _mainScrollView.backgroundColor = [UIColor whiteColor];
  166. _mainScrollView.pagingEnabled = YES;
  167. _mainScrollView.showsHorizontalScrollIndicator = NO;
  168. _mainScrollView.showsVerticalScrollIndicator = NO;
  169. [self.view addSubview:_mainScrollView];
  170. NSArray *views = @[self.goodsSearchVC.view,self.orderSearchVC.view];
  171. for (NSInteger i = 0; i< views.count; i++) {
  172. //把三个vc的view依次贴到mainScrollView上面
  173. UIView *pageView = [[UIView alloc]initWithFrame:CGRectMake(Screen_Width * i, 0, _mainScrollView.frame.size.width, _mainScrollView.frame.size.height - 100)];
  174. [pageView addSubview:views[i]];
  175. [_mainScrollView addSubview:pageView];
  176. }
  177. _mainScrollView.contentSize = CGSizeMake(Screen_Width*(views.count), 0);
  178. //滚动到_currentIndex对应的tab
  179. [_mainScrollView setContentOffset:CGPointMake((_mainScrollView.frame.size.width)*_currentIndex, 0) animated:YES];
  180. }
  181. /**
  182. 滑动label位置
  183. @param scrollView <#scrollView description#>
  184. */
  185. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  186. //实时计算当前位置,实现和titleView上的按钮的联动
  187. CGFloat contentOffSetX = scrollView.contentOffset.x;
  188. CGFloat X = contentOffSetX/ navcount;
  189. CGRect frame = _sliderLabel.frame;
  190. frame.origin.x = X;
  191. _sliderLabel.frame = frame;
  192. }
  193. /**
  194. 滑动完成
  195. @param scrollView <#scrollView description#>
  196. */
  197. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  198. CGFloat contentOffSetX = scrollView.contentOffset.x;
  199. int index_ = contentOffSetX/Screen_Width;
  200. [self sliderWithTag:index_+1];
  201. }
  202. /**
  203. 滑动tag
  204. @param tag <#tag description#>
  205. */
  206. - (void)sliderWithTag:(NSInteger)tag{
  207. self.currentIndex = tag;
  208. _productSearchButton.selected = NO;
  209. _frozenOrderButton.selected = NO;
  210. UIButton *sender = [self buttonWithTag:tag];
  211. sender.selected = YES;
  212. //动画
  213. _sliderLabel.frame = CGRectMake(sender.frame.origin.x, _sliderLabel.frame.origin.y, _sliderLabel.frame.size.width, _sliderLabel.frame.size.height);
  214. }
  215. @end