DeliveryReceiptHomeViewController.m 8.3 KB

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