InstallReceiptHomeViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // InstallReceiptHomeViewController.m
  3. // IBOSS
  4. //
  5. // Created by guan hong hou on 2017/7/10.
  6. // Copyright © 2017年 沈阳东科云信软件有限公司. All rights reserved.
  7. //
  8. // 功能描述:安装回执主页视图控制器
  9. #import "InstallReceiptHomeViewController.h"
  10. @interface InstallReceiptHomeViewController ()
  11. @end
  12. @implementation InstallReceiptHomeViewController
  13. //导航数
  14. static int navcount = 2;
  15. #pragma mark - 公共函数
  16. /**
  17. 视图加载函数
  18. */
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.navigationItem.title = @"安装回执一览";
  22. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  23. [button setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal];
  24. [button addTarget:self action:@selector(goBack)
  25. forControlEvents:UIControlEventTouchUpInside];
  26. button.frame = CGRectMake(0, 0,45,22);
  27. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  28. self.navigationItem.leftBarButtonItem = menuButton;
  29. [self initUI];
  30. [self setMainSrollView];
  31. [self sliderWithTag:self.currentIndex+1];
  32. }
  33. /**
  34. 安全区视图发生变化
  35. */
  36. -(void)viewSafeAreaInsetsDidChange{
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. _mainScrollView.frame = CGRectMake(0, 40, Screen_Width,self.view.safeAreaLayoutGuide.layoutFrame.size.height-40);
  39. for (NSInteger i = 0; i < _mainScrollView.subviews.count; i++) {
  40. _mainScrollView.subviews[i].frame = CGRectMake(Screen_Width * i, 0, Screen_Width,CGRectGetHeight(_mainScrollView.frame));
  41. }
  42. [self.installUnreceiptVC viewSafeAreaInsetsDidChange];
  43. [self.installReceiptVC viewSafeAreaInsetsDidChange];
  44. [super viewSafeAreaInsetsDidChange];
  45. }
  46. /**
  47. 初始化安装未回执控制器
  48. @return <#return value description#>
  49. */
  50. - (InstallUnreceiptVC *)installUnreceiptVC{
  51. if (_installUnreceiptVC == nil) {
  52. _installUnreceiptVC = [[InstallUnreceiptVC alloc] init];
  53. _installUnreceiptVC.cNav = self.navigationController;
  54. }
  55. return _installUnreceiptVC;
  56. }
  57. /**
  58. 初始化安装回执控制器
  59. @return <#return value description#>
  60. */
  61. - (InstallReceiptedVC *)installReceiptVC{
  62. if (_installReceiptVC == nil) {
  63. _installReceiptVC = [[InstallReceiptedVC alloc] init];
  64. _installReceiptVC.cNav = self.navigationController;
  65. }
  66. return _installReceiptVC;
  67. }
  68. #pragma mark - 委托函数
  69. /**
  70. 滑动label位置
  71. @param scrollView <#scrollView description#>
  72. */
  73. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  74. //实时计算当前位置,实现和titleView上的按钮的联动
  75. CGFloat contentOffSetX = scrollView.contentOffset.x;
  76. CGFloat x = contentOffSetX / navcount;
  77. CGRect frame = _sliderLabel.frame;
  78. frame.origin.x = x;
  79. _sliderLabel.frame = frame;
  80. }
  81. /**
  82. 滑动完成
  83. @param scrollView <#scrollView description#>
  84. */
  85. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  86. CGFloat contentOffSetX = scrollView.contentOffset.x;
  87. int index = contentOffSetX / Screen_Width;
  88. [self sliderWithTag:index + 1];
  89. }
  90. #pragma mark - 私有函数
  91. /*
  92. 初始化UI
  93. */
  94. - (void)initUI{
  95. UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
  96. [self.view addSubview:navView];
  97. [self.view setBackgroundColor:[UIColor whiteColor]];
  98. _installUnreceiptButton = [UIButton buttonWithType:UIButtonTypeCustom];
  99. [_installUnreceiptButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  100. [_installUnreceiptButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  101. _installUnreceiptButton.frame = CGRectMake(0, 0, Screen_Width / navcount, navView.frame.size.height);
  102. _installUnreceiptButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  103. [_installUnreceiptButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  104. [_installUnreceiptButton setTitle:@"未回执" forState:UIControlStateNormal];
  105. _installUnreceiptButton.tag = 1;
  106. _installUnreceiptButton.selected = YES;
  107. [navView addSubview:_installUnreceiptButton];
  108. _installReceiptedButton= [UIButton buttonWithType:UIButtonTypeCustom];
  109. _installReceiptedButton.frame = CGRectMake(CGRectGetMaxX(_installUnreceiptButton.frame),0, Screen_Width / navcount, navView.frame.size.height);
  110. [_installReceiptedButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  111. [_installReceiptedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  112. _installReceiptedButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  113. [_installReceiptedButton addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  114. [_installReceiptedButton setTitle:@"已回执" forState:UIControlStateNormal];
  115. _installReceiptedButton.titleLabel.font = [UIFont systemFontOfSize:ButtonFontOfSize];
  116. _installReceiptedButton.tag = 2;
  117. [navView addSubview: _installReceiptedButton];
  118. _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(_installUnreceiptButton.frame.origin.x, 40 - 2, Screen_Width / navcount, 2)];
  119. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(((Screen_Width/navcount)-50)/2, 0, 50, 2)];
  120. line.backgroundColor = [UIColor redColor];
  121. [_sliderLabel addSubview:line];
  122. [navView addSubview:_sliderLabel];
  123. }
  124. /**
  125. 滑动tag
  126. @param tag <#tag description#>
  127. */
  128. - (void)sliderWithTag:(NSInteger)tag{
  129. self.currentIndex = tag;
  130. _installUnreceiptButton.selected = NO;
  131. _installReceiptedButton.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. _installReceiptedButton.selected = NO;
  159. _installUnreceiptButton.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 _installUnreceiptButton;
  174. }
  175. else if (tag == 2){
  176. return _installReceiptedButton;
  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.installUnreceiptVC.view,self.installReceiptVC.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