RepairReceiptHomeViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // RepairReceiptHomeViewController.m
  3. // IBOSS
  4. //
  5. // Created by guan hong hou on 2017/7/10.
  6. // Copyright © 2017年 沈阳东科云信软件有限公司. All rights reserved.
  7. //
  8. // 功能描述:维修回执主页控制器
  9. #import "RepairReceiptHomeViewController.h"
  10. @interface RepairReceiptHomeViewController ()
  11. @end
  12. @implementation RepairReceiptHomeViewController
  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.repairUnreceiptVC viewSafeAreaInsetsDidChange];
  42. [self.repairReceiptedVC viewSafeAreaInsetsDidChange];
  43. [super viewSafeAreaInsetsDidChange];
  44. }
  45. /**
  46. 维修未回执vc
  47. @return <#return value description#>
  48. */
  49. - (RepairUnreceiptVC *)repairUnreceiptVC{
  50. if (_repairUnreceiptVC == nil) {
  51. _repairUnreceiptVC = [[RepairUnreceiptVC alloc] init];
  52. _repairUnreceiptVC.cNav = self.navigationController;
  53. }
  54. return _repairUnreceiptVC;
  55. }
  56. /**
  57. 维修已回执vc
  58. @return <#return value description#>
  59. */
  60. - (RepairReceiptedVC *)repairReceiptedVC{
  61. if (_repairReceiptedVC == nil) {
  62. _repairReceiptedVC = [[RepairReceiptedVC alloc] init];
  63. _repairReceiptedVC.cNav = self.navigationController;
  64. }
  65. return _repairReceiptedVC;
  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. _repairUnreceiptButton = [UIButton buttonWithType:UIButtonTypeCustom];
  98. [_repairUnreceiptButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  99. [_repairUnreceiptButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  100. _repairUnreceiptButton.frame = CGRectMake(0, 0, Screen_Width / navcount, navView.frame.size.height);
  101. _repairUnreceiptButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  102. [_repairUnreceiptButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  103. [_repairUnreceiptButton setTitle:@"未回执" forState:UIControlStateNormal];
  104. _repairUnreceiptButton.tag = 1;
  105. _repairUnreceiptButton.selected = YES;
  106. [navView addSubview:_repairUnreceiptButton];
  107. _repairReceiptedButton = [UIButton buttonWithType:UIButtonTypeCustom];
  108. _repairReceiptedButton.frame = CGRectMake(CGRectGetMaxX(_repairUnreceiptButton.frame),0, Screen_Width / navcount, navView.frame.size.height);
  109. [_repairReceiptedButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  110. [_repairReceiptedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  111. _repairReceiptedButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  112. [_repairReceiptedButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  113. [_repairReceiptedButton setTitle:@"已回执" forState:UIControlStateNormal];
  114. _repairReceiptedButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  115. _repairReceiptedButton.tag = 2;
  116. [navView addSubview: _repairReceiptedButton];
  117. _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(_repairReceiptedButton.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. [navView addSubview:_sliderLabel];
  123. }
  124. /**
  125. 滑动tag
  126. @param tag <#tag description#>
  127. */
  128. - (void)sliderWithTag:(NSInteger)tag{
  129. self.currentIndex = tag;
  130. _repairUnreceiptButton.selected = NO;
  131. _repairReceiptedButton.selected = NO;
  132. UIButton *sender = [self buttonWithTag:tag];
  133. sender.selected = YES;
  134. //动画
  135. _sliderLabel.frame = CGRectMake(sender.frame.origin.x, _sliderLabel.frame.origin.y, _sliderLabel.frame.size.width, _sliderLabel.frame.size.height);
  136. }
  137. /**
  138. 按钮点击事件
  139. @param sender <#sender description#>
  140. */
  141. - (void)sliderAction:(UIButton *)sender{
  142. if (self.currentIndex == sender.tag) {
  143. return;
  144. }
  145. [self.view endEditing:YES];
  146. [self sliderAnimationWithTag:sender.tag];
  147. [UIView animateWithDuration:0.3 animations:^{
  148. _mainScrollView.contentOffset = CGPointMake(Screen_Width*(sender.tag - 1), 0);
  149. } completion:^(BOOL finished) {
  150. }];
  151. }
  152. /**
  153. 滑动tag
  154. @param tag <#tag description#>
  155. */
  156. - (void)sliderAnimationWithTag:(NSInteger)tag{
  157. self.currentIndex = tag;
  158. _repairReceiptedButton.selected = NO;
  159. _repairUnreceiptButton.selected = NO;
  160. UIButton *sender = [self buttonWithTag:tag];
  161. sender.selected = YES;
  162. //动画
  163. [UIView animateWithDuration:0.3 animations:^{
  164. _sliderLabel.frame = CGRectMake(sender.frame.origin.x, _sliderLabel.frame.origin.y, _sliderLabel.frame.size.width, _sliderLabel.frame.size.height);
  165. } completion:^(BOOL finished) {
  166. }];
  167. }
  168. /*
  169. 获得导航按钮
  170. */
  171. - (UIButton *)buttonWithTag:(NSInteger )tag{
  172. if (tag == 1) {
  173. return _repairUnreceiptButton;
  174. }
  175. else if (tag == 2){
  176. return _repairReceiptedButton;
  177. }
  178. else{
  179. return nil;
  180. }
  181. }
  182. /**
  183. 初始化srollView
  184. */
  185. - (void)setMainSrollView{
  186. _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, Screen_Width, self.view.frame.size.height)];
  187. _mainScrollView.delegate = self;
  188. _mainScrollView.backgroundColor = [UIColor whiteColor];
  189. _mainScrollView.pagingEnabled = YES;
  190. _mainScrollView.showsHorizontalScrollIndicator = NO;
  191. _mainScrollView.showsVerticalScrollIndicator = NO;
  192. [self.view addSubview:_mainScrollView];
  193. NSArray *views = @[self.repairUnreceiptVC.view,self.repairReceiptedVC.view];
  194. for (NSInteger i = 0; i< views.count; i++) {
  195. //把三个vc的view依次贴到mainScrollView上面
  196. UIView *pageView = [[UIView alloc]initWithFrame:CGRectMake(Screen_Width * i, 0, _mainScrollView.frame.size.width, _mainScrollView.frame.size.height - 100)];
  197. [pageView addSubview:views[i]];
  198. [_mainScrollView addSubview:pageView];
  199. }
  200. _mainScrollView.contentSize = CGSizeMake(Screen_Width*(views.count), 0);
  201. //滚动到_currentIndex对应的tab
  202. [_mainScrollView setContentOffset:CGPointMake((_mainScrollView.frame.size.width)*_currentIndex, 0) animated:YES];
  203. }
  204. /**
  205. 返回函数
  206. */
  207. - (void)goBack{
  208. [self.navigationController popViewControllerAnimated:YES];
  209. }
  210. @end