OrderHomeViewController.m 8.7 KB

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