JXPagerListContainerView.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //
  2. // JXPagerListContainerView.m
  3. // JXCategoryView
  4. //
  5. // Created by jiaxin on 2018/9/12.
  6. // Copyright © 2018年 jiaxin. All rights reserved.
  7. //
  8. #import "JXPagerListContainerView.h"
  9. #import <objc/runtime.h>
  10. @interface JXPagerListContainerScrollView: UIScrollView <UIGestureRecognizerDelegate>
  11. @property (nonatomic, assign, getter=isCategoryNestPagingEnabled) BOOL categoryNestPagingEnabled;
  12. @end
  13. @implementation JXPagerListContainerScrollView
  14. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  15. if (self.isCategoryNestPagingEnabled) {
  16. if ([gestureRecognizer isMemberOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")]) {
  17. CGFloat velocityX = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:gestureRecognizer.view].x;
  18. //x大于0就是往右滑
  19. if (velocityX > 0) {
  20. if (self.contentOffset.x == 0) {
  21. return NO;
  22. }
  23. }else if (velocityX < 0) {
  24. //x小于0就是往左滑
  25. if (self.contentOffset.x + self.bounds.size.width == self.contentSize.width) {
  26. return NO;
  27. }
  28. }
  29. }
  30. }
  31. return YES;
  32. }
  33. @end
  34. @interface JXPagerListContainerCollectionView: UICollectionView <UIGestureRecognizerDelegate>
  35. @property (nonatomic, assign, getter=isCategoryNestPagingEnabled) BOOL categoryNestPagingEnabled;
  36. @end
  37. @implementation JXPagerListContainerCollectionView
  38. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  39. if (self.isCategoryNestPagingEnabled) {
  40. if ([gestureRecognizer isMemberOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")]) {
  41. CGFloat velocityX = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:gestureRecognizer.view].x;
  42. //x大于0就是往右滑
  43. if (velocityX > 0) {
  44. if (self.contentOffset.x == 0) {
  45. return NO;
  46. }
  47. }else if (velocityX < 0) {
  48. //x小于0就是往左滑
  49. if (self.contentOffset.x + self.bounds.size.width == self.contentSize.width) {
  50. return NO;
  51. }
  52. }
  53. }
  54. }
  55. return YES;
  56. }
  57. @end
  58. @interface JXPagerListContainerViewController : UIViewController
  59. @property (copy) void(^viewWillAppearBlock)(void);
  60. @property (copy) void(^viewDidAppearBlock)(void);
  61. @property (copy) void(^viewWillDisappearBlock)(void);
  62. @property (copy) void(^viewDidDisappearBlock)(void);
  63. @end
  64. @implementation JXPagerListContainerViewController
  65. - (void)dealloc
  66. {
  67. self.viewWillAppearBlock = nil;
  68. self.viewDidAppearBlock = nil;
  69. self.viewWillDisappearBlock = nil;
  70. self.viewDidDisappearBlock = nil;
  71. }
  72. - (void)viewWillAppear:(BOOL)animated {
  73. [super viewWillAppear:animated];
  74. self.viewWillAppearBlock();
  75. }
  76. - (void)viewDidAppear:(BOOL)animated {
  77. [super viewDidAppear:animated];
  78. self.viewDidAppearBlock();
  79. }
  80. - (void)viewWillDisappear:(BOOL)animated {
  81. [super viewWillDisappear:animated];
  82. self.viewWillDisappearBlock();
  83. }
  84. - (void)viewDidDisappear:(BOOL)animated {
  85. [super viewDidDisappear:animated];
  86. self.viewDidDisappearBlock();
  87. }
  88. - (BOOL)shouldAutomaticallyForwardAppearanceMethods { return NO; }
  89. @end
  90. @interface JXPagerListContainerView () <UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
  91. @property (nonatomic, weak) id<JXPagerListContainerViewDelegate> delegate;
  92. @property (nonatomic, strong) UIScrollView *scrollView;
  93. @property (nonatomic, assign) NSInteger currentIndex;
  94. @property (nonatomic, strong) NSMutableDictionary <NSNumber *, id<JXPagerViewListViewDelegate>> *validListDict;
  95. @property (nonatomic, assign) NSInteger willAppearIndex;
  96. @property (nonatomic, assign) NSInteger willDisappearIndex;
  97. @property (nonatomic, strong) UICollectionView *collectionView;
  98. @property (nonatomic, strong) JXPagerListContainerViewController *containerVC;
  99. @end
  100. @implementation JXPagerListContainerView
  101. - (instancetype)initWithType:(JXPagerListContainerType)type delegate:(id<JXPagerListContainerViewDelegate>)delegate{
  102. self = [super initWithFrame:CGRectZero];
  103. if (self) {
  104. _containerType = type;
  105. _delegate = delegate;
  106. _validListDict = [NSMutableDictionary dictionary];
  107. _willAppearIndex = -1;
  108. _willDisappearIndex = -1;
  109. _initListPercent = 0.01;
  110. [self initializeViews];
  111. }
  112. return self;
  113. }
  114. - (void)initializeViews {
  115. _listCellBackgroundColor = [UIColor whiteColor];
  116. _containerVC = [[JXPagerListContainerViewController alloc] init];
  117. self.containerVC.view.backgroundColor = [UIColor clearColor];
  118. [self addSubview:self.containerVC.view];
  119. __weak typeof(self) weakSelf = self;
  120. self.containerVC.viewWillAppearBlock = ^{
  121. [weakSelf listWillAppear:weakSelf.currentIndex];
  122. };
  123. self.containerVC.viewDidAppearBlock = ^{
  124. [weakSelf listDidAppear:weakSelf.currentIndex];
  125. };
  126. self.containerVC.viewWillDisappearBlock = ^{
  127. [weakSelf listWillDisappear:weakSelf.currentIndex];
  128. };
  129. self.containerVC.viewDidDisappearBlock = ^{
  130. [weakSelf listDidDisappear:weakSelf.currentIndex];
  131. };
  132. if (self.containerType == JXPagerListContainerType_ScrollView) {
  133. if (self.delegate &&
  134. [self.delegate respondsToSelector:@selector(scrollViewClassInlistContainerView:)] &&
  135. [[self.delegate scrollViewClassInlistContainerView:self] isKindOfClass:object_getClass([UIScrollView class])]) {
  136. _scrollView = (UIScrollView *)[[[self.delegate scrollViewClassInlistContainerView:self] alloc] init];
  137. }else {
  138. _scrollView = [[JXPagerListContainerScrollView alloc] init];
  139. }
  140. self.scrollView.backgroundColor = [UIColor clearColor];
  141. self.scrollView.delegate = self;
  142. self.scrollView.pagingEnabled = YES;
  143. self.scrollView.showsHorizontalScrollIndicator = NO;
  144. self.scrollView.showsVerticalScrollIndicator = NO;
  145. self.scrollView.scrollsToTop = NO;
  146. self.scrollView.bounces = NO;
  147. if (@available(iOS 11.0, *)) {
  148. self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  149. }
  150. [self.containerVC.view addSubview:self.scrollView];
  151. }else {
  152. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  153. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  154. layout.minimumLineSpacing = 0;
  155. layout.minimumInteritemSpacing = 0;
  156. if (self.delegate &&
  157. [self.delegate respondsToSelector:@selector(scrollViewClassInlistContainerView:)] &&
  158. [[self.delegate scrollViewClassInlistContainerView:self] isKindOfClass:object_getClass([UICollectionView class])]) {
  159. _collectionView = (UICollectionView *)[[[self.delegate scrollViewClassInlistContainerView:self] alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  160. }else {
  161. _collectionView = [[JXPagerListContainerCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  162. }
  163. self.collectionView.backgroundColor = [UIColor clearColor];
  164. self.collectionView.pagingEnabled = YES;
  165. self.collectionView.showsHorizontalScrollIndicator = NO;
  166. self.collectionView.showsVerticalScrollIndicator = NO;
  167. self.collectionView.scrollsToTop = NO;
  168. self.collectionView.bounces = NO;
  169. self.collectionView.dataSource = self;
  170. self.collectionView.delegate = self;
  171. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
  172. if (@available(iOS 10.0, *)) {
  173. self.collectionView.prefetchingEnabled = NO;
  174. }
  175. if (@available(iOS 11.0, *)) {
  176. self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  177. }
  178. [self.containerVC.view addSubview:self.collectionView];
  179. //让外部统一访问scrollView
  180. _scrollView = _collectionView;
  181. }
  182. }
  183. - (void)willMoveToSuperview:(UIView *)newSuperview {
  184. [super willMoveToSuperview:newSuperview];
  185. UIResponder *next = newSuperview;
  186. while (next != nil) {
  187. if ([next isKindOfClass:[UIViewController class]]) {
  188. [((UIViewController *)next) addChildViewController:self.containerVC];
  189. break;
  190. }
  191. next = next.nextResponder;
  192. }
  193. }
  194. - (void)layoutSubviews {
  195. [super layoutSubviews];
  196. self.containerVC.view.frame = self.bounds;
  197. if (self.containerType == JXPagerListContainerType_ScrollView) {
  198. if (CGRectEqualToRect(self.scrollView.frame, CGRectZero) || !CGSizeEqualToSize(self.scrollView.bounds.size, self.bounds.size)) {
  199. self.scrollView.frame = self.bounds;
  200. self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width*[self.delegate numberOfListsInlistContainerView:self], self.scrollView.bounds.size.height);
  201. [_validListDict enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull index, id<JXPagerViewListViewDelegate> _Nonnull list, BOOL * _Nonnull stop) {
  202. [list listView].frame = CGRectMake(index.intValue*self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
  203. }];
  204. self.scrollView.contentOffset = CGPointMake(self.currentIndex*self.scrollView.bounds.size.width, 0);
  205. }else {
  206. self.scrollView.frame = self.bounds;
  207. self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width*[self.delegate numberOfListsInlistContainerView:self], self.scrollView.bounds.size.height);
  208. }
  209. }else {
  210. if (CGRectEqualToRect(self.collectionView.frame, CGRectZero) || !CGSizeEqualToSize(self.collectionView.bounds.size, self.bounds.size)) {
  211. self.collectionView.frame = self.bounds;
  212. [self.collectionView.collectionViewLayout invalidateLayout];
  213. [self.collectionView reloadData];
  214. [self.collectionView setContentOffset:CGPointMake(self.collectionView.bounds.size.width*self.currentIndex, 0) animated:NO];
  215. }else {
  216. self.collectionView.frame = self.bounds;
  217. }
  218. }
  219. }
  220. - (void)setinitListPercent:(CGFloat)initListPercent {
  221. _initListPercent = initListPercent;
  222. if (initListPercent <= 0 || initListPercent >= 1) {
  223. NSAssert(NO, @"initListPercent值范围为开区间(0,1),即不包括0和1");
  224. }
  225. }
  226. - (void)setCategoryNestPagingEnabled:(BOOL)categoryNestPagingEnabled {
  227. _categoryNestPagingEnabled = categoryNestPagingEnabled;
  228. if ([self.scrollView isKindOfClass:[JXPagerListContainerScrollView class]]) {
  229. ((JXPagerListContainerScrollView *)self.scrollView).categoryNestPagingEnabled = categoryNestPagingEnabled;
  230. }else if ([self.scrollView isKindOfClass:[JXPagerListContainerCollectionView class]]) {
  231. ((JXPagerListContainerCollectionView *)self.scrollView).categoryNestPagingEnabled = categoryNestPagingEnabled;
  232. }
  233. }
  234. #pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
  235. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  236. return [self.delegate numberOfListsInlistContainerView:self];
  237. }
  238. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  239. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  240. cell.contentView.backgroundColor = self.listCellBackgroundColor;
  241. for (UIView *subview in cell.contentView.subviews) {
  242. [subview removeFromSuperview];
  243. }
  244. id<JXPagerViewListViewDelegate> list = _validListDict[@(indexPath.item)];
  245. if (list != nil) {
  246. //fixme:如果list是UIViewController,如果这里的frame修改是`[list listView].frame = cell.bounds;`。那么就必须给list vc添加如下代码:
  247. //- (void)loadView {
  248. // self.view = [[UIView alloc] init];
  249. //}
  250. //所以,总感觉是把UIViewController当做普通view使用,导致了系统内部的bug。所以,缓兵之计就是用下面的方法,暂时解决问题。
  251. if ([list isKindOfClass:[UIViewController class]]) {
  252. [list listView].frame = cell.contentView.bounds;
  253. } else {
  254. [list listView].frame = cell.bounds;
  255. }
  256. [cell.contentView addSubview:[list listView]];
  257. }
  258. return cell;
  259. }
  260. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  261. return self.bounds.size;
  262. }
  263. #pragma mark - UIScrollViewDelegate
  264. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  265. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerViewDidScroll:)]) {
  266. [self.delegate listContainerViewDidScroll:scrollView];
  267. }
  268. if (!scrollView.isDragging && !scrollView.isTracking && !scrollView.isDecelerating) {
  269. return;
  270. }
  271. CGFloat ratio = scrollView.contentOffset.x/scrollView.bounds.size.width;
  272. NSInteger maxCount = round(scrollView.contentSize.width/scrollView.bounds.size.width);
  273. NSInteger leftIndex = floorf(ratio);
  274. leftIndex = MAX(0, MIN(maxCount - 1, leftIndex));
  275. NSInteger rightIndex = leftIndex + 1;
  276. if (ratio < 0 || rightIndex >= maxCount) {
  277. [self listDidAppearOrDisappear:scrollView];
  278. return;
  279. }
  280. CGFloat remainderRatio = ratio - leftIndex;
  281. if (rightIndex == self.currentIndex) {
  282. //当前选中的在右边,用户正在从右边往左边滑动
  283. if (self.validListDict[@(leftIndex)] == nil && remainderRatio < (1 - self.initListPercent)) {
  284. [self initListIfNeededAtIndex:leftIndex];
  285. }else if (self.validListDict[@(leftIndex)] != nil) {
  286. if (self.willAppearIndex == -1) {
  287. self.willAppearIndex = leftIndex;
  288. [self listWillAppear:self.willAppearIndex];
  289. }
  290. }
  291. if (self.willDisappearIndex == -1) {
  292. self.willDisappearIndex = rightIndex;
  293. [self listWillDisappear:self.willDisappearIndex];
  294. }
  295. }else {
  296. //当前选中的在左边,用户正在从左边往右边滑动
  297. if (self.validListDict[@(rightIndex)] == nil && remainderRatio > self.initListPercent) {
  298. [self initListIfNeededAtIndex:rightIndex];
  299. }else if (self.validListDict[@(rightIndex)] != nil) {
  300. if (self.willAppearIndex == -1) {
  301. self.willAppearIndex = rightIndex;
  302. [self listWillAppear:self.willAppearIndex];
  303. }
  304. }
  305. if (self.willDisappearIndex == -1) {
  306. self.willDisappearIndex = leftIndex;
  307. [self listWillDisappear:self.willDisappearIndex];
  308. }
  309. }
  310. [self listDidAppearOrDisappear:scrollView];
  311. }
  312. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  313. if (self.willDisappearIndex != -1) {
  314. [self listWillAppear:self.willDisappearIndex];
  315. [self listWillDisappear:self.willAppearIndex];
  316. [self listDidAppear:self.willDisappearIndex];
  317. [self listDidDisappear:self.willAppearIndex];
  318. self.willDisappearIndex = -1;
  319. self.willAppearIndex = -1;
  320. }
  321. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerViewWDidEndScroll:)]) {
  322. [self.delegate listContainerViewWDidEndScroll:self];
  323. }
  324. }
  325. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  326. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerViewWillBeginDragging:)]) {
  327. [self.delegate listContainerViewWillBeginDragging:self];
  328. }
  329. }
  330. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  331. if (!decelerate) {
  332. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerViewWDidEndScroll:)]) {
  333. [self.delegate listContainerViewWDidEndScroll:self];
  334. }
  335. }
  336. }
  337. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  338. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerViewWDidEndScroll:)]) {
  339. [self.delegate listContainerViewWDidEndScroll:self];
  340. }
  341. }
  342. #pragma mark - JXCategoryViewListContainer
  343. - (UIScrollView *)contentScrollView {
  344. return self.scrollView;
  345. }
  346. - (void)setDefaultSelectedIndex:(NSInteger)index {
  347. self.currentIndex = index;
  348. }
  349. - (void)scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio selectedIndex:(NSInteger)selectedIndex {
  350. }
  351. - (void)didClickSelectedItemAtIndex:(NSInteger)index {
  352. if (![self checkIndexValid:index]) {
  353. return;
  354. }
  355. self.willAppearIndex = -1;
  356. self.willDisappearIndex = -1;
  357. if (self.currentIndex != index) {
  358. [self listWillDisappear:self.currentIndex];
  359. [self listDidDisappear:self.currentIndex];
  360. [self listWillAppear:index];
  361. [self listDidAppear:index];
  362. }
  363. }
  364. - (void)reloadData {
  365. for (id<JXPagerViewListViewDelegate> list in _validListDict.allValues) {
  366. [[list listView] removeFromSuperview];
  367. if ([list isKindOfClass:[UIViewController class]]) {
  368. [(UIViewController *)list removeFromParentViewController];
  369. }
  370. }
  371. [_validListDict removeAllObjects];
  372. if (self.containerType == JXPagerListContainerType_ScrollView) {
  373. self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width*[self.delegate numberOfListsInlistContainerView:self], self.scrollView.bounds.size.height);
  374. }else {
  375. [self.collectionView reloadData];
  376. }
  377. [self listWillAppear:self.currentIndex];
  378. [self listDidAppear:self.currentIndex];
  379. }
  380. #pragma mark - Private
  381. - (void)initListIfNeededAtIndex:(NSInteger)index {
  382. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:canInitListAtIndex:)]) {
  383. BOOL canInitList = [self.delegate listContainerView:self canInitListAtIndex:index];
  384. if (!canInitList) {
  385. return;
  386. }
  387. }
  388. id<JXPagerViewListViewDelegate> list = _validListDict[@(index)];
  389. if (list != nil) {
  390. //列表已经创建好了
  391. return;
  392. }
  393. list = [self.delegate listContainerView:self initListForIndex:index];
  394. if ([list isKindOfClass:[UIViewController class]]) {
  395. [self.containerVC addChildViewController:(UIViewController *)list];
  396. }
  397. _validListDict[@(index)] = list;
  398. switch (self.containerType) {
  399. case JXPagerListContainerType_ScrollView: {
  400. [list listView].frame = CGRectMake(index*self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
  401. [self.scrollView addSubview:[list listView]];
  402. break;
  403. }
  404. case JXPagerListContainerType_CollectionView: {
  405. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  406. if (cell != nil) {
  407. for (UIView *subview in cell.contentView.subviews) {
  408. [subview removeFromSuperview];
  409. }
  410. [list listView].frame = cell.contentView.bounds;
  411. [cell.contentView addSubview:[list listView]];
  412. }
  413. break;
  414. }
  415. }
  416. }
  417. - (void)listWillAppear:(NSInteger)index {
  418. if (![self checkIndexValid:index]) {
  419. return;
  420. }
  421. id<JXPagerViewListViewDelegate> list = _validListDict[@(index)];
  422. if (list != nil) {
  423. if (list && [list respondsToSelector:@selector(listWillAppear)]) {
  424. [list listWillAppear];
  425. }
  426. if ([list isKindOfClass:[UIViewController class]]) {
  427. UIViewController *listVC = (UIViewController *)list;
  428. [listVC beginAppearanceTransition:YES animated:NO];
  429. }
  430. }else {
  431. //当前列表未被创建(页面初始化或通过点击触发的listWillAppear)
  432. BOOL canInitList = YES;
  433. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:canInitListAtIndex:)]) {
  434. canInitList = [self.delegate listContainerView:self canInitListAtIndex:index];
  435. }
  436. if (canInitList) {
  437. id<JXPagerViewListViewDelegate> list = _validListDict[@(index)];
  438. if (list == nil) {
  439. list = [self.delegate listContainerView:self initListForIndex:index];
  440. if ([list isKindOfClass:[UIViewController class]]) {
  441. [self.containerVC addChildViewController:(UIViewController *)list];
  442. }
  443. _validListDict[@(index)] = list;
  444. }
  445. if (self.containerType == JXPagerListContainerType_ScrollView) {
  446. if ([list listView].superview == nil) {
  447. [list listView].frame = CGRectMake(index*self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
  448. [self.scrollView addSubview:[list listView]];
  449. if (list && [list respondsToSelector:@selector(listWillAppear)]) {
  450. [list listWillAppear];
  451. }
  452. if ([list isKindOfClass:[UIViewController class]]) {
  453. UIViewController *listVC = (UIViewController *)list;
  454. [listVC beginAppearanceTransition:YES animated:NO];
  455. }
  456. }
  457. }else {
  458. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  459. for (UIView *subview in cell.contentView.subviews) {
  460. [subview removeFromSuperview];
  461. }
  462. [list listView].frame = cell.contentView.bounds;
  463. [cell.contentView addSubview:[list listView]];
  464. if (list && [list respondsToSelector:@selector(listWillAppear)]) {
  465. [list listWillAppear];
  466. }
  467. if ([list isKindOfClass:[UIViewController class]]) {
  468. UIViewController *listVC = (UIViewController *)list;
  469. [listVC beginAppearanceTransition:YES animated:NO];
  470. }
  471. }
  472. }
  473. }
  474. }
  475. - (void)listDidAppear:(NSInteger)index {
  476. if (![self checkIndexValid:index]) {
  477. return;
  478. }
  479. self.currentIndex = index;
  480. id<JXPagerViewListViewDelegate> list = _validListDict[@(index)];
  481. if (list && [list respondsToSelector:@selector(listDidAppear)]) {
  482. [list listDidAppear];
  483. }
  484. if ([list isKindOfClass:[UIViewController class]]) {
  485. UIViewController *listVC = (UIViewController *)list;
  486. [listVC endAppearanceTransition];
  487. }
  488. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:listDidAppearAtIndex:)]) {
  489. [self.delegate listContainerView:self listDidAppearAtIndex:index];
  490. }
  491. }
  492. - (void)listWillDisappear:(NSInteger)index {
  493. if (![self checkIndexValid:index]) {
  494. return;
  495. }
  496. id<JXPagerViewListViewDelegate> list = _validListDict[@(index)];
  497. if (list && [list respondsToSelector:@selector(listWillDisappear)]) {
  498. [list listWillDisappear];
  499. }
  500. if ([list isKindOfClass:[UIViewController class]]) {
  501. UIViewController *listVC = (UIViewController *)list;
  502. [listVC beginAppearanceTransition:NO animated:NO];
  503. }
  504. }
  505. - (void)listDidDisappear:(NSInteger)index {
  506. if (![self checkIndexValid:index]) {
  507. return;
  508. }
  509. id<JXPagerViewListViewDelegate> list = _validListDict[@(index)];
  510. if (list && [list respondsToSelector:@selector(listDidDisappear)]) {
  511. [list listDidDisappear];
  512. }
  513. if ([list isKindOfClass:[UIViewController class]]) {
  514. UIViewController *listVC = (UIViewController *)list;
  515. [listVC endAppearanceTransition];
  516. }
  517. }
  518. - (BOOL)checkIndexValid:(NSInteger)index {
  519. NSUInteger count = [self.delegate numberOfListsInlistContainerView:self];
  520. if (count <= 0 || index >= count) {
  521. return NO;
  522. }
  523. return YES;
  524. }
  525. - (void)listDidAppearOrDisappear:(UIScrollView *)scrollView {
  526. CGFloat currentIndexPercent = scrollView.contentOffset.x/scrollView.bounds.size.width;
  527. if (self.willAppearIndex != -1 || self.willDisappearIndex != -1) {
  528. NSInteger disappearIndex = self.willDisappearIndex;
  529. NSInteger appearIndex = self.willAppearIndex;
  530. if (self.willAppearIndex > self.willDisappearIndex) {
  531. //将要出现的列表在右边
  532. if (currentIndexPercent >= self.willAppearIndex) {
  533. self.willDisappearIndex = -1;
  534. self.willAppearIndex = -1;
  535. [self listDidDisappear:disappearIndex];
  536. [self listDidAppear:appearIndex];
  537. }
  538. }else {
  539. //将要出现的列表在左边
  540. if (currentIndexPercent <= self.willAppearIndex) {
  541. self.willDisappearIndex = -1;
  542. self.willAppearIndex = -1;
  543. [self listDidDisappear:disappearIndex];
  544. [self listDidAppear:appearIndex];
  545. }
  546. }
  547. }
  548. }
  549. @end