新UI马甲包

FSPageContentView.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // FSPageContentView.m
  3. // Huim
  4. //
  5. // Created by huim on 2017/4/28.
  6. // Copyright © 2017年 huim. All rights reserved.
  7. //
  8. #import "FSPageContentView.h"
  9. #define IOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
  10. static NSString *collectionCellIdentifier = @"collectionCellIdentifier";
  11. @interface FSPageContentView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  12. @property (nonatomic, weak) UIViewController *parentVC;//父视图
  13. @property (nonatomic, strong) NSArray *childsVCs;//子视图数组
  14. @property (nonatomic, weak) UICollectionView *collectionView;
  15. @property (nonatomic, assign) CGFloat startOffsetX;
  16. @property (nonatomic, assign) BOOL isSelectBtn;//是否是滑动
  17. @end
  18. @implementation FSPageContentView
  19. - (instancetype)initWithFrame:(CGRect)frame childVCs:(NSArray *)childVCs parentVC:(UIViewController *)parentVC delegate:(id<FSPageContentViewDelegate>)delegate
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. self.parentVC = parentVC;
  24. self.childsVCs = childVCs;
  25. self.delegate = delegate;
  26. [self setupSubViews];
  27. }
  28. return self;
  29. }
  30. - (void)layoutSubviews
  31. {
  32. [super layoutSubviews];
  33. }
  34. #pragma mark --LazyLoad
  35. - (UICollectionView *)collectionView
  36. {
  37. if (!_collectionView) {
  38. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  39. flowLayout.itemSize = self.bounds.size;
  40. flowLayout.minimumLineSpacing = 0;
  41. flowLayout.minimumInteritemSpacing = 0;
  42. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  43. UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
  44. collectionView.showsHorizontalScrollIndicator = NO;
  45. collectionView.showsVerticalScrollIndicator=NO;
  46. collectionView.pagingEnabled = YES;
  47. collectionView.bounces = NO;
  48. collectionView.delegate = self;
  49. collectionView.dataSource = self;
  50. [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:collectionCellIdentifier];
  51. [self addSubview:collectionView];
  52. self.collectionView = collectionView;
  53. }
  54. return _collectionView;
  55. }
  56. #pragma mark --setup
  57. - (void)setupSubViews
  58. {
  59. _startOffsetX = 0;
  60. _isSelectBtn = NO;
  61. _contentViewCanScroll = YES;
  62. for (UIViewController *childVC in self.childsVCs) {
  63. [self.parentVC addChildViewController:childVC];
  64. }
  65. // [self addSubview:self.collectionView];
  66. [self.collectionView reloadData];
  67. }
  68. #pragma mark UICollectionView
  69. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  70. {
  71. return self.childsVCs.count;
  72. }
  73. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  74. {
  75. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellIdentifier forIndexPath:indexPath];
  76. if (IOS_VERSION < 8.0) {
  77. [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  78. UIViewController *childVC = self.childsVCs[indexPath.item];
  79. childVC.view.frame = cell.contentView.bounds;
  80. [cell.contentView addSubview:childVC.view];
  81. }
  82. return cell;
  83. }
  84. #ifdef __IPHONE_8_0
  85. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
  86. [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  87. UIViewController *childVc = self.childsVCs[indexPath.row];
  88. childVc.view.frame = cell.contentView.bounds;
  89. [cell.contentView addSubview:childVc.view];
  90. }
  91. #endif
  92. #pragma mark UIScrollView
  93. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  94. {
  95. _isSelectBtn = NO;
  96. _startOffsetX = scrollView.contentOffset.x;
  97. if (self.delegate && [self.delegate respondsToSelector:@selector(FSContentViewWillBeginDragging:)]) {
  98. [self.delegate FSContentViewWillBeginDragging:self];
  99. }
  100. }
  101. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  102. {
  103. if (_isSelectBtn) {
  104. return;
  105. }
  106. CGFloat scrollView_W = scrollView.bounds.size.width;
  107. CGFloat currentOffsetX = scrollView.contentOffset.x;
  108. NSInteger startIndex = floor(_startOffsetX/scrollView_W);
  109. NSInteger endIndex;
  110. CGFloat progress;
  111. if (currentOffsetX > _startOffsetX) {//左滑left
  112. progress = (currentOffsetX - _startOffsetX)/scrollView_W;
  113. endIndex = startIndex + 1;
  114. if (endIndex > self.childsVCs.count - 1) {
  115. endIndex = self.childsVCs.count - 1;
  116. }
  117. }else if (currentOffsetX == _startOffsetX){//没滑过去
  118. progress = 0;
  119. endIndex = startIndex;
  120. }else{//右滑right
  121. progress = (_startOffsetX - currentOffsetX)/scrollView_W;
  122. endIndex = startIndex - 1;
  123. endIndex = endIndex < 0?0:endIndex;
  124. }
  125. if (self.delegate && [self.delegate respondsToSelector:@selector(FSContentViewDidScroll:startIndex:endIndex:progress:)]) {
  126. [self.delegate FSContentViewDidScroll:self startIndex:startIndex endIndex:endIndex progress:progress];
  127. }
  128. }
  129. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  130. {
  131. CGFloat scrollView_W = scrollView.bounds.size.width;
  132. CGFloat currentOffsetX = scrollView.contentOffset.x;
  133. NSInteger startIndex = floor(_startOffsetX/scrollView_W);
  134. NSInteger endIndex = floor(currentOffsetX/scrollView_W);
  135. if (self.delegate && [self.delegate respondsToSelector:@selector(FSContenViewDidEndDecelerating:startIndex:endIndex:)]) {
  136. [self.delegate FSContenViewDidEndDecelerating:self startIndex:startIndex endIndex:endIndex];
  137. }
  138. }
  139. #pragma mark setter
  140. - (void)setContentViewCurrentIndex:(NSInteger)contentViewCurrentIndex
  141. {
  142. if (_contentViewCurrentIndex < 0||_contentViewCurrentIndex > self.childsVCs.count-1) {
  143. return;
  144. }
  145. _isSelectBtn = YES;
  146. _contentViewCurrentIndex = contentViewCurrentIndex;
  147. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:contentViewCurrentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  148. }
  149. - (void)setContentViewCanScroll:(BOOL)contentViewCanScroll
  150. {
  151. _contentViewCanScroll = contentViewCanScroll;
  152. _collectionView.scrollEnabled = _contentViewCanScroll;
  153. }
  154. @end