JXPagerSmoothView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // JXPagerSmoothView.m
  3. // JXPagerViewExample-OC
  4. //
  5. // Created by jiaxin on 2019/11/15.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. #import "JXPagerSmoothView.h"
  9. static NSString *JXPagerSmoothViewCollectionViewCellIdentifier = @"cell";
  10. @interface JXPagerSmoothCollectionView : UICollectionView <UIGestureRecognizerDelegate>
  11. @property (nonatomic, strong) UIView *pagerHeaderContainerView;
  12. @end
  13. @implementation JXPagerSmoothCollectionView
  14. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  15. CGPoint point = [touch locationInView:self.pagerHeaderContainerView];
  16. if (CGRectContainsPoint(self.pagerHeaderContainerView.bounds, point)) {
  17. return NO;
  18. }
  19. return YES;
  20. }
  21. @end
  22. @interface JXPagerSmoothView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  23. @property (nonatomic, weak) id<JXPagerSmoothViewDataSource> dataSource;
  24. @property (nonatomic, strong) JXPagerSmoothCollectionView *listCollectionView;
  25. @property (nonatomic, strong) NSMutableDictionary <NSNumber *, id<JXPagerSmoothViewListViewDelegate>> *listDict;
  26. @property (nonatomic, strong) NSMutableDictionary <NSNumber *, UIView*> *listHeaderDict;
  27. @property (nonatomic, assign, getter=isSyncListContentOffsetEnabled) BOOL syncListContentOffsetEnabled;
  28. @property (nonatomic, strong) UIView *pagerHeaderContainerView;
  29. @property (nonatomic, assign) CGFloat currentPagerHeaderContainerViewY;
  30. @property (nonatomic, assign) NSInteger currentIndex;
  31. @property (nonatomic, strong) UIScrollView *currentListScrollView;
  32. @property (nonatomic, assign) CGFloat heightForPagerHeader;
  33. @property (nonatomic, assign) CGFloat heightForPinHeader;
  34. @property (nonatomic, assign) CGFloat heightForPagerHeaderContainerView;
  35. @property (nonatomic, assign) CGFloat currentListInitializeContentOffsetY;
  36. @property (nonatomic, strong) UIScrollView *singleScrollView;
  37. @end
  38. @implementation JXPagerSmoothView
  39. - (void)dealloc
  40. {
  41. for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
  42. [[list listScrollView] removeObserver:self forKeyPath:@"contentOffset"];
  43. [[list listScrollView] removeObserver:self forKeyPath:@"contentSize"];
  44. }
  45. }
  46. - (instancetype)initWithDataSource:(id<JXPagerSmoothViewDataSource>)dataSource
  47. {
  48. self = [super initWithFrame:CGRectZero];
  49. if (self) {
  50. _dataSource = dataSource;
  51. _listDict = [NSMutableDictionary dictionary];
  52. _listHeaderDict = [NSMutableDictionary dictionary];
  53. [self initializeViews];
  54. }
  55. return self;
  56. }
  57. - (void)initializeViews {
  58. self.pagerHeaderContainerView = [[UIView alloc] init];
  59. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  60. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  61. layout.minimumLineSpacing = 0;
  62. layout.minimumInteritemSpacing = 0;
  63. _listCollectionView = [[JXPagerSmoothCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  64. self.listCollectionView.dataSource = self;
  65. self.listCollectionView.delegate = self;
  66. self.listCollectionView.pagingEnabled = YES;
  67. self.listCollectionView.bounces = NO;
  68. self.listCollectionView.showsHorizontalScrollIndicator = NO;
  69. self.listCollectionView.scrollsToTop = NO;
  70. [self.listCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:JXPagerSmoothViewCollectionViewCellIdentifier];
  71. if (@available(iOS 10.0, *)) {
  72. self.listCollectionView.prefetchingEnabled = NO;
  73. }
  74. if (@available(iOS 11.0, *)) {
  75. self.listCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  76. }
  77. _listCollectionView.pagerHeaderContainerView = self.pagerHeaderContainerView;
  78. [self addSubview:self.listCollectionView];
  79. }
  80. - (void)layoutSubviews {
  81. [super layoutSubviews];
  82. self.listCollectionView.frame = self.bounds;
  83. if (CGRectEqualToRect(self.pagerHeaderContainerView.frame, CGRectZero)) {
  84. [self reloadData];
  85. }
  86. if (self.singleScrollView != nil) {
  87. self.singleScrollView.frame = self.bounds;
  88. }
  89. }
  90. - (void)reloadData {
  91. self.currentListScrollView = nil;
  92. self.currentIndex = self.defaultSelectedIndex;
  93. self.currentPagerHeaderContainerViewY = 0;
  94. self.syncListContentOffsetEnabled = NO;
  95. [self.listHeaderDict removeAllObjects];
  96. for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
  97. [[list listScrollView] removeObserver:self forKeyPath:@"contentOffset"];
  98. [[list listScrollView] removeObserver:self forKeyPath:@"contentSize"];
  99. [[list listView] removeFromSuperview];
  100. }
  101. [_listDict removeAllObjects];
  102. self.heightForPagerHeader = [self.dataSource heightForPagerHeaderInPagerView:self];
  103. self.heightForPinHeader = [self.dataSource heightForPinHeaderInPagerView:self];
  104. self.heightForPagerHeaderContainerView = self.heightForPagerHeader + self.heightForPinHeader;
  105. UIView *pagerHeader = [self.dataSource viewForPagerHeaderInPagerView:self];
  106. UIView *pinHeader = [self.dataSource viewForPinHeaderInPagerView:self];
  107. [self.pagerHeaderContainerView addSubview:pagerHeader];
  108. [self.pagerHeaderContainerView addSubview:pinHeader];
  109. self.pagerHeaderContainerView.frame = CGRectMake(0, 0, self.bounds.size.width, self.heightForPagerHeaderContainerView);
  110. pagerHeader.frame = CGRectMake(0, 0, self.bounds.size.width, self.heightForPagerHeader);
  111. pinHeader.frame = CGRectMake(0, self.heightForPagerHeader, self.bounds.size.width, self.heightForPinHeader);
  112. [self.listCollectionView setContentOffset:CGPointMake(self.listCollectionView.bounds.size.width*self.defaultSelectedIndex, 0) animated:NO];
  113. [self.listCollectionView reloadData];
  114. if ([self.dataSource numberOfListsInPagerView:self] == 0) {
  115. self.singleScrollView = [[UIScrollView alloc] init];
  116. [self addSubview:self.singleScrollView];
  117. [self.singleScrollView addSubview:pagerHeader];
  118. self.singleScrollView.contentSize = CGSizeMake(self.bounds.size.width, self.heightForPagerHeader);
  119. }else if (self.singleScrollView != nil) {
  120. [self.singleScrollView removeFromSuperview];
  121. self.singleScrollView = nil;
  122. }
  123. }
  124. #pragma mark - UICollectionViewDataSource & UICollectionViewDelegateFlowLayout
  125. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  126. return self.bounds.size;
  127. }
  128. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  129. return [self.dataSource numberOfListsInPagerView:self];
  130. }
  131. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  132. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JXPagerSmoothViewCollectionViewCellIdentifier forIndexPath:indexPath];
  133. id<JXPagerSmoothViewListViewDelegate> list = self.listDict[@(indexPath.item)];
  134. if (list == nil) {
  135. list = [self.dataSource pagerView:self initListAtIndex:indexPath.item];
  136. _listDict[@(indexPath.item)] = list;
  137. [[list listView] setNeedsLayout];
  138. [[list listView] layoutIfNeeded];
  139. UIScrollView *listScrollView = [list listScrollView];
  140. if ([listScrollView isKindOfClass:[UITableView class]]) {
  141. ((UITableView *)listScrollView).estimatedRowHeight = 0;
  142. ((UITableView *)listScrollView).estimatedSectionFooterHeight = 0;
  143. ((UITableView *)listScrollView).estimatedSectionHeaderHeight = 0;
  144. }
  145. if (@available(iOS 11.0, *)) {
  146. listScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  147. }
  148. listScrollView.contentInset = UIEdgeInsetsMake(self.heightForPagerHeaderContainerView, 0, 0, 0);
  149. self.currentListInitializeContentOffsetY = -listScrollView.contentInset.top + MIN(-self.currentPagerHeaderContainerViewY, self.heightForPagerHeader);
  150. listScrollView.contentOffset = CGPointMake(0, self.currentListInitializeContentOffsetY);
  151. UIView *listHeader = [[UIView alloc] initWithFrame:CGRectMake(0, -self.heightForPagerHeaderContainerView, self.bounds.size.width, self.heightForPagerHeaderContainerView)];
  152. [listScrollView addSubview:listHeader];
  153. if (self.pagerHeaderContainerView.superview == nil) {
  154. [listHeader addSubview:self.pagerHeaderContainerView];
  155. }
  156. self.listHeaderDict[@(indexPath.item)] = listHeader;
  157. [listScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
  158. [listScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
  159. }
  160. for (id<JXPagerSmoothViewListViewDelegate> listItem in self.listDict.allValues) {
  161. [listItem listScrollView].scrollsToTop = (listItem == list);
  162. }
  163. UIView *listView = [list listView];
  164. if (listView != nil && listView.superview != cell.contentView) {
  165. for (UIView *view in cell.contentView.subviews) {
  166. [view removeFromSuperview];
  167. }
  168. listView.frame = cell.contentView.bounds;
  169. [cell.contentView addSubview:listView];
  170. }
  171. return cell;
  172. }
  173. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  174. [self listDidAppear:indexPath.item];
  175. }
  176. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  177. [self listDidDisappear:indexPath.item];
  178. }
  179. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  180. if (self.delegate && [self.delegate respondsToSelector:@selector(pagerSmoothViewDidScroll:)]) {
  181. [self.delegate pagerSmoothViewDidScroll:scrollView];
  182. }
  183. CGFloat indexPercent = scrollView.contentOffset.x/scrollView.bounds.size.width;
  184. NSInteger index = floor(indexPercent);
  185. UIScrollView *listScrollView = [self.listDict[@(index)] listScrollView];
  186. if (indexPercent - index == 0 && index != self.currentIndex && !(scrollView.isDragging || scrollView.isDecelerating) && listScrollView.contentOffset.y <= -self.heightForPinHeader) {
  187. [self horizontalScrollDidEndAtIndex:index];
  188. }else {
  189. //左右滚动的时候,就把listHeaderContainerView添加到self,达到悬浮在顶部的效果
  190. if (self.pagerHeaderContainerView.superview != self) {
  191. self.pagerHeaderContainerView.frame = CGRectMake(0, self.currentPagerHeaderContainerViewY, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
  192. [self addSubview:self.pagerHeaderContainerView];
  193. }
  194. }
  195. if (index != self.currentIndex) {
  196. self.currentIndex = index;
  197. }
  198. }
  199. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  200. if (!decelerate) {
  201. NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
  202. [self horizontalScrollDidEndAtIndex:index];
  203. }
  204. }
  205. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  206. NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
  207. [self horizontalScrollDidEndAtIndex:index];
  208. }
  209. #pragma mark - KVO
  210. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  211. if ([keyPath isEqualToString:@"contentOffset"]) {
  212. UIScrollView *scrollView = (UIScrollView *)object;
  213. if (scrollView != nil) {
  214. [self listDidScroll:scrollView];
  215. }
  216. }else if([keyPath isEqualToString:@"contentSize"]) {
  217. UIScrollView *scrollView = (UIScrollView *)object;
  218. if (scrollView != nil) {
  219. CGFloat minContentSizeHeight = self.bounds.size.height - self.heightForPinHeader;
  220. if (minContentSizeHeight > scrollView.contentSize.height) {
  221. scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, minContentSizeHeight);
  222. //新的scrollView第一次加载的时候重置contentOffset
  223. if (_currentListScrollView != nil && scrollView != _currentListScrollView) {
  224. scrollView.contentOffset = CGPointMake(0, self.currentListInitializeContentOffsetY);
  225. }
  226. }
  227. }
  228. }else {
  229. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  230. }
  231. }
  232. #pragma mark - Event
  233. - (void)listDidScroll:(UIScrollView *)scrollView {
  234. if (self.listCollectionView.isDragging || self.listCollectionView.isDecelerating) {
  235. return;
  236. }
  237. NSInteger listIndex = [self listIndexForListScrollView:scrollView];
  238. if (listIndex != self.currentIndex) {
  239. return;
  240. }
  241. self.currentListScrollView = scrollView;
  242. CGFloat contentOffsetY = scrollView.contentOffset.y + self.heightForPagerHeaderContainerView;
  243. if (contentOffsetY < self.heightForPagerHeader) {
  244. self.syncListContentOffsetEnabled = YES;
  245. self.currentPagerHeaderContainerViewY = -contentOffsetY;
  246. for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
  247. if ([list listScrollView] != self.currentListScrollView) {
  248. [[list listScrollView] setContentOffset:scrollView.contentOffset animated:NO];
  249. }
  250. }
  251. UIView *listHeader = [self listHeaderForListScrollView:scrollView];
  252. if (self.pagerHeaderContainerView.superview != listHeader) {
  253. self.pagerHeaderContainerView.frame = CGRectMake(0, 0, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
  254. [listHeader addSubview:self.pagerHeaderContainerView];
  255. }
  256. }else {
  257. if (self.pagerHeaderContainerView.superview != self) {
  258. self.pagerHeaderContainerView.frame = CGRectMake(0, -self.heightForPagerHeader, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
  259. [self addSubview:self.pagerHeaderContainerView];
  260. }
  261. if (self.isSyncListContentOffsetEnabled) {
  262. self.syncListContentOffsetEnabled = NO;
  263. self.currentPagerHeaderContainerViewY = -self.heightForPagerHeader;
  264. for (id<JXPagerSmoothViewListViewDelegate> list in self.listDict.allValues) {
  265. if ([list listScrollView] != scrollView) {
  266. [[list listScrollView] setContentOffset:CGPointMake(0, -self.heightForPinHeader) animated:NO];
  267. }
  268. }
  269. }
  270. }
  271. }
  272. #pragma mark - Private
  273. - (UIView *)listHeaderForListScrollView:(UIScrollView *)scrollView {
  274. for (NSNumber *index in self.listDict) {
  275. if ([self.listDict[index] listScrollView] == scrollView) {
  276. return self.listHeaderDict[index];
  277. }
  278. }
  279. return nil;
  280. }
  281. - (NSInteger)listIndexForListScrollView:(UIScrollView *)scrollView {
  282. for (NSNumber *index in self.listDict) {
  283. if ([self.listDict[index] listScrollView] == scrollView) {
  284. return [index integerValue];
  285. }
  286. }
  287. return 0;
  288. }
  289. - (void)listDidAppear:(NSInteger)index {
  290. NSUInteger count = [self.dataSource numberOfListsInPagerView:self];
  291. if (count <= 0 || index >= count) {
  292. return;
  293. }
  294. id<JXPagerSmoothViewListViewDelegate> list = self.listDict[@(index)];
  295. if (list && [list respondsToSelector:@selector(listDidAppear)]) {
  296. [list listDidAppear];
  297. }
  298. }
  299. - (void)listDidDisappear:(NSInteger)index {
  300. NSUInteger count = [self.dataSource numberOfListsInPagerView:self];
  301. if (count <= 0 || index >= count) {
  302. return;
  303. }
  304. id<JXPagerSmoothViewListViewDelegate> list = self.listDict[@(index)];
  305. if (list && [list respondsToSelector:@selector(listDidDisappear)]) {
  306. [list listDidDisappear];
  307. }
  308. }
  309. /// 列表左右切换滚动结束之后,需要把pagerHeaderContainerView添加到当前index的列表上面
  310. - (void)horizontalScrollDidEndAtIndex:(NSInteger)index {
  311. self.currentIndex = index;
  312. UIView *listHeader = self.listHeaderDict[@(index)];
  313. UIScrollView *listScrollView = [self.listDict[@(index)] listScrollView];
  314. if (listHeader != nil && listScrollView.contentOffset.y <= -self.heightForPinHeader) {
  315. for (id<JXPagerSmoothViewListViewDelegate> listItem in self.listDict.allValues) {
  316. [listItem listScrollView].scrollsToTop = ([listItem listScrollView] == listScrollView);
  317. }
  318. self.pagerHeaderContainerView.frame = CGRectMake(0, 0, self.pagerHeaderContainerView.bounds.size.width, self.pagerHeaderContainerView.bounds.size.height);
  319. [listHeader addSubview:self.pagerHeaderContainerView];
  320. }
  321. }
  322. @end