CGXVerticalMenuListContainerView.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. //
  2. // CGXVerticalMenuListContainerView.m
  3. // CGXVerticalMenuView-OC
  4. //
  5. // Created by CGX on 2018/05/01.
  6. // Copyright © 2019 CGX. All rights reserved.
  7. //
  8. #import "CGXVerticalMenuListContainerView.h"
  9. #import <objc/runtime.h>
  10. #import "CGXVerticalMenuListContainerViewController.h"
  11. @interface CGXVerticalMenuListContainerView () <UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
  12. @property (nonatomic, weak) id<CGXVerticalMenuListContainerViewDataSource> delegate;
  13. @property (nonatomic, assign,readwrite) NSInteger currentIndex;
  14. @property (nonatomic, strong) NSMutableDictionary <NSNumber *, id<CGXVerticalMenuListContainerViewDelegate>> *validListDict;
  15. @property (nonatomic, assign) NSInteger willAppearIndex;
  16. @property (nonatomic, assign) NSInteger willDisappearIndex;
  17. @property (nonatomic, strong) UICollectionView *collectionView;
  18. @property (nonatomic, strong) CGXVerticalMenuListContainerViewController *containerVC;
  19. @property (nonatomic, strong) UICollectionViewFlowLayout *layout;
  20. @end
  21. @implementation CGXVerticalMenuListContainerView
  22. - (instancetype)initWithDelegate:(id<CGXVerticalMenuListContainerViewDataSource>)delegate
  23. {
  24. self = [super initWithFrame:CGRectZero];
  25. if (self) {
  26. _delegate = delegate;
  27. self.backgroundColor = [UIColor whiteColor];
  28. _validListDict = [NSMutableDictionary dictionary];
  29. _willAppearIndex = -1;
  30. _willDisappearIndex = -1;
  31. _initListPercent = 0.01;
  32. self.isHorizontal = NO;
  33. [self initializeViews];
  34. }
  35. return self;
  36. }
  37. - (void)setIsHorizontal:(BOOL)isHorizontal
  38. {
  39. _isHorizontal = isHorizontal;
  40. if (self.isHorizontal) {
  41. self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  42. } else{
  43. self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  44. }
  45. self.collectionView.collectionViewLayout = self.layout;
  46. [self.collectionView.collectionViewLayout invalidateLayout];
  47. [self.collectionView reloadData];
  48. }
  49. - (void)initializeViews {
  50. _containerVC = [[CGXVerticalMenuListContainerViewController alloc] init];
  51. self.containerVC.view.backgroundColor = [UIColor clearColor];
  52. [self addSubview:self.containerVC.view];
  53. __weak typeof(self) weakSelf = self;
  54. self.containerVC.viewWillAppearBlock = ^{
  55. [weakSelf listWillAppear:weakSelf.currentIndex];
  56. };
  57. self.containerVC.viewDidAppearBlock = ^{
  58. [weakSelf listDidAppear:weakSelf.currentIndex];
  59. };
  60. self.containerVC.viewWillDisappearBlock = ^{
  61. [weakSelf listWillDisappear:weakSelf.currentIndex];
  62. };
  63. self.containerVC.viewDidDisappearBlock = ^{
  64. [weakSelf listDidDisappear:weakSelf.currentIndex];
  65. };
  66. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  67. if (self.isHorizontal) {
  68. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  69. } else{
  70. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  71. }
  72. layout.minimumLineSpacing = 0;
  73. layout.minimumInteritemSpacing = 0;
  74. self.layout = layout;
  75. if (self.delegate &&
  76. [self.delegate respondsToSelector:@selector(scrollViewClassInlistContainerView:)] &&
  77. [[self.delegate scrollViewClassInlistContainerView:self] isKindOfClass:object_getClass([UICollectionView class])]) {
  78. _collectionView = (UICollectionView *)[[[self.delegate scrollViewClassInlistContainerView:self] alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  79. }else {
  80. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  81. }
  82. self.collectionView.backgroundColor = self.backgroundColor;
  83. self.collectionView.pagingEnabled = YES;
  84. self.collectionView.showsHorizontalScrollIndicator = NO;
  85. self.collectionView.showsVerticalScrollIndicator = NO;
  86. self.collectionView.scrollsToTop = NO;
  87. self.collectionView.bounces = NO;
  88. self.collectionView.dataSource = self;
  89. self.collectionView.delegate = self;
  90. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass( [UICollectionViewCell class])];
  91. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
  92. if (@available(iOS 10.0, *)) {
  93. self.collectionView.prefetchingEnabled = NO;
  94. }
  95. if (@available(iOS 11.0, *)) {
  96. if ([self.collectionView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
  97. self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  98. }
  99. }
  100. [self.containerVC.view addSubview:self.collectionView];
  101. }
  102. - (void)willMoveToSuperview:(UIView *)newSuperview {
  103. [super willMoveToSuperview:newSuperview];
  104. UIResponder *next = newSuperview;
  105. while (next != nil) {
  106. if ([next isKindOfClass:[UIViewController class]]) {
  107. [((UIViewController *)next) addChildViewController:self.containerVC];
  108. break;
  109. }
  110. next = next.nextResponder;
  111. }
  112. }
  113. - (void)layoutSubviews {
  114. [super layoutSubviews];
  115. self.containerVC.view.frame = self.bounds;
  116. self.collectionView.backgroundColor = self.backgroundColor;
  117. if (CGRectEqualToRect(self.collectionView.frame, CGRectZero) || !CGSizeEqualToSize(self.collectionView.bounds.size, self.bounds.size)) {
  118. [self.collectionView.collectionViewLayout invalidateLayout];
  119. [self.collectionView reloadData];
  120. }
  121. self.collectionView.frame = self.bounds;
  122. [self scrollSelectedItemAtIndex:self.currentIndex];
  123. [self.collectionView reloadData];
  124. }
  125. - (void)setinitListPercent:(CGFloat)initListPercent {
  126. _initListPercent = initListPercent;
  127. if (initListPercent <= 0 || initListPercent >= 1) {
  128. NSAssert(NO, @"initListPercent值范围为开区间(0,1),即不包括0和1");
  129. }
  130. }
  131. - (void)setBounces:(BOOL)bounces {
  132. _bounces = bounces;
  133. self.collectionView.bounces = bounces;
  134. }
  135. #pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
  136. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  137. return [self.delegate numberOfListsInlistContainerView:self];
  138. }
  139. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  140. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
  141. cell.contentView.backgroundColor = self.backgroundColor;
  142. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(indexPath.item)];
  143. BOOL canInitList = YES;
  144. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:canInitListAtIndex:)]) {
  145. canInitList = [self.delegate listContainerView:self canInitListAtIndex:indexPath.item];
  146. }
  147. if (canInitList) {
  148. if (list == nil && self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:initListForIndex:)]) {
  149. list = [self.delegate listContainerView:self initListForIndex:indexPath.item];
  150. if (list != nil) {
  151. _validListDict[@(indexPath.item)] = list;
  152. }
  153. }
  154. for (UIView *subview in cell.contentView.subviews) {
  155. [subview removeFromSuperview];
  156. }
  157. if (list != nil) {
  158. [list listView].frame = cell.contentView.bounds;
  159. [cell.contentView addSubview:[list listView]];
  160. }
  161. }
  162. return cell;
  163. }
  164. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  165. return self.bounds.size;
  166. }
  167. #pragma mark - UIScrollViewDelegate
  168. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  169. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerViewDidScroll:)]) {
  170. [self.delegate listContainerViewDidScroll:scrollView];
  171. }
  172. if (!scrollView.isDragging && !scrollView.isTracking && !scrollView.isDecelerating) {
  173. return;
  174. }
  175. CGFloat ratio = scrollView.contentOffset.y/scrollView.bounds.size.height;
  176. NSInteger maxCount = round(scrollView.contentSize.height/scrollView.bounds.size.height);
  177. if (self.isHorizontal) {
  178. ratio = scrollView.contentOffset.x/scrollView.bounds.size.width;
  179. maxCount = round(scrollView.contentSize.width/scrollView.bounds.size.width);
  180. }
  181. NSInteger leftIndex = floorf(ratio);
  182. leftIndex = MAX(0, MIN(maxCount - 1, leftIndex));
  183. NSInteger rightIndex = leftIndex + 1;
  184. if (ratio < 0 || rightIndex >= maxCount) {
  185. [self listDidAppearOrDisappear:scrollView];
  186. return;
  187. }
  188. CGFloat remainderRatio = ratio - leftIndex;
  189. if (rightIndex == self.currentIndex) {
  190. //当前选中的在右边,用户正在从右边往左边滑动
  191. if (self.validListDict[@(leftIndex)] == nil && remainderRatio < (1 - self.initListPercent)) {
  192. [self initListIfNeededAtIndex:leftIndex];
  193. }else if (self.validListDict[@(leftIndex)] != nil) {
  194. if (self.willAppearIndex == -1) {
  195. self.willAppearIndex = leftIndex;
  196. [self listWillAppear:self.willAppearIndex];
  197. }
  198. }
  199. if (self.willDisappearIndex == -1) {
  200. self.willDisappearIndex = rightIndex;
  201. [self listWillDisappear:self.willDisappearIndex];
  202. }
  203. }else {
  204. //当前选中的在左边,用户正在从左边往右边滑动
  205. if (self.validListDict[@(rightIndex)] == nil && remainderRatio > self.initListPercent) {
  206. [self initListIfNeededAtIndex:rightIndex];
  207. }else if (self.validListDict[@(rightIndex)] != nil) {
  208. if (self.willAppearIndex == -1) {
  209. self.willAppearIndex = rightIndex;
  210. [self listWillAppear:self.willAppearIndex];
  211. }
  212. }
  213. if (self.willDisappearIndex == -1) {
  214. self.willDisappearIndex = leftIndex;
  215. [self listWillDisappear:self.willDisappearIndex];
  216. }
  217. }
  218. [self listDidAppearOrDisappear:scrollView];
  219. }
  220. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  221. //滑动到一半又取消滑动处理
  222. if (self.willDisappearIndex != -1) {
  223. [self listWillAppear:self.willDisappearIndex];
  224. [self listWillDisappear:self.willAppearIndex];
  225. [self listDidAppear:self.willDisappearIndex];
  226. [self listDidDisappear:self.willAppearIndex];
  227. self.willDisappearIndex = -1;
  228. self.willAppearIndex = -1;
  229. }
  230. }
  231. #pragma mark - Private
  232. - (void)initListIfNeededAtIndex:(NSInteger)index {
  233. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:canInitListAtIndex:)]) {
  234. BOOL canInitList = [self.delegate listContainerView:self canInitListAtIndex:index];
  235. if (!canInitList) {
  236. return;
  237. }
  238. }
  239. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  240. if (list != nil) {
  241. //列表已经创建好了
  242. return;
  243. }
  244. list = [self.delegate listContainerView:self initListForIndex:index];
  245. if ([list isKindOfClass:[UIViewController class]]) {
  246. [self.containerVC addChildViewController:(UIViewController *)list];
  247. }
  248. _validListDict[@(index)] = list;
  249. UICollectionViewCell *cell = (UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  250. for (UIView *subview in cell.contentView.subviews) {
  251. [subview removeFromSuperview];
  252. }
  253. if (list != nil) {
  254. [list listView].frame = cell.contentView.bounds;
  255. [cell.contentView addSubview:[list listView]];
  256. }
  257. }
  258. - (void)listWillAppear:(NSInteger)index {
  259. if (![self checkIndexValid:index]) {
  260. return;
  261. }
  262. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  263. if (list != nil) {
  264. [self listWillVCAppearAtIndex:index];
  265. }else {
  266. //当前列表未被创建(页面初始化或通过点击触发的listWillAppear)
  267. BOOL canInitList = YES;
  268. if (self.delegate && [self.delegate respondsToSelector:@selector(listContainerView:canInitListAtIndex:)]) {
  269. canInitList = [self.delegate listContainerView:self canInitListAtIndex:index];
  270. }
  271. if (canInitList) {
  272. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  273. if (list == nil) {
  274. list = [self.delegate listContainerView:self initListForIndex:index];
  275. if ([list isKindOfClass:[UIViewController class]]) {
  276. [self.containerVC addChildViewController:(UIViewController *)list];
  277. }
  278. _validListDict[@(index)] = list;
  279. }
  280. UICollectionViewCell *cell = (UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
  281. for (UIView *subview in cell.contentView.subviews) {
  282. [subview removeFromSuperview];
  283. }
  284. if (list != nil) {
  285. [list listView].frame = cell.contentView.bounds;
  286. [cell.contentView addSubview:[list listView]];
  287. }
  288. if (list && [list respondsToSelector:@selector(listWillAppearAtIndex:)]) {
  289. [list listWillAppearAtIndex:index];
  290. }
  291. if ([list isKindOfClass:[UIViewController class]]) {
  292. UIViewController *listVC = (UIViewController *)list;
  293. [listVC beginAppearanceTransition:YES animated:NO];
  294. }
  295. }
  296. }
  297. }
  298. - (void)listDidAppear:(NSInteger)index {
  299. if (![self checkIndexValid:index]) {
  300. return;
  301. }
  302. self.currentIndex = index;
  303. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  304. if (list && [list respondsToSelector:@selector(listDidAppearAtIndex:)]) {
  305. [list listDidAppearAtIndex:index];
  306. }
  307. if ([list isKindOfClass:[UIViewController class]]) {
  308. UIViewController *listVC = (UIViewController *)list;
  309. [listVC endAppearanceTransition];
  310. }
  311. }
  312. - (void)listWillDisappear:(NSInteger)index {
  313. if (![self checkIndexValid:index]) {
  314. return;
  315. }
  316. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  317. if (list && [list respondsToSelector:@selector(listWillDisappearAtIndex:)]) {
  318. [list listWillDisappearAtIndex:index];
  319. }
  320. if ([list isKindOfClass:[UIViewController class]]) {
  321. UIViewController *listVC = (UIViewController *)list;
  322. [listVC beginAppearanceTransition:NO animated:NO];
  323. }
  324. }
  325. - (void)listDidDisappear:(NSInteger)index {
  326. if (![self checkIndexValid:index]) {
  327. return;
  328. }
  329. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  330. if (list && [list respondsToSelector:@selector(listDidDisappearAtIndex:)]) {
  331. [list listDidDisappearAtIndex:index];
  332. }
  333. if ([list isKindOfClass:[UIViewController class]]) {
  334. UIViewController *listVC = (UIViewController *)list;
  335. [listVC endAppearanceTransition];
  336. }
  337. }
  338. - (BOOL)checkIndexValid:(NSInteger)index {
  339. NSUInteger count = [self.delegate numberOfListsInlistContainerView:self];
  340. if (count <= 0 || index >= count) {
  341. return NO;
  342. }
  343. return YES;
  344. }
  345. - (void)listDidAppearOrDisappear:(UIScrollView *)scrollView {
  346. CGFloat currentIndexPercent = scrollView.contentOffset.y/scrollView.bounds.size.height;
  347. if (self.isHorizontal) {
  348. currentIndexPercent = scrollView.contentOffset.x/scrollView.bounds.size.width;
  349. }
  350. if (self.willAppearIndex != -1 || self.willDisappearIndex != -1) {
  351. NSInteger disappearIndex = self.willDisappearIndex;
  352. NSInteger appearIndex = self.willAppearIndex;
  353. if (self.willAppearIndex > self.willDisappearIndex) {
  354. //将要出现的列表在右边
  355. if (currentIndexPercent >= self.willAppearIndex) {
  356. self.willDisappearIndex = -1;
  357. self.willAppearIndex = -1;
  358. [self listDidDisappear:disappearIndex];
  359. [self listDidAppear:appearIndex];
  360. }
  361. }else {
  362. //将要出现的列表在左边
  363. if (currentIndexPercent <= self.willAppearIndex) {
  364. self.willDisappearIndex = -1;
  365. self.willAppearIndex = -1;
  366. [self listDidDisappear:disappearIndex];
  367. [self listDidAppear:appearIndex];
  368. }
  369. }
  370. }
  371. }
  372. - (void)listWillVCAppearAtIndex:(NSInteger)index
  373. {
  374. if (![self checkIndexValid:index]) {
  375. return;
  376. }
  377. id<CGXVerticalMenuListContainerViewDelegate> list = _validListDict[@(index)];
  378. if (list && [list respondsToSelector:@selector(listWillAppearAtIndex:)]) {
  379. [list listWillAppearAtIndex:index];
  380. }
  381. if ([list isKindOfClass:[UIViewController class]]) {
  382. UIViewController *listVC = (UIViewController *)list;
  383. [listVC beginAppearanceTransition:YES animated:NO];
  384. }
  385. }
  386. - (UIScrollView *)contentScrollView {
  387. return self.collectionView;
  388. }
  389. - (void)setDefaultSelectedIndex:(NSInteger)index {
  390. self.currentIndex = index;
  391. }
  392. - (void)scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio selectedIndex:(NSInteger)selectedIndex {
  393. if (rightIndex == selectedIndex) {
  394. //当前选中的在右边,用户正在从右边往左边滑动
  395. if (ratio < (1 - self.initListPercent)) {
  396. [self initListIfNeededAtIndex:leftIndex];
  397. }
  398. if (self.willAppearIndex == -1) {
  399. self.willAppearIndex = leftIndex;
  400. if (self.validListDict[@(leftIndex)] != nil) {
  401. [self listWillAppear:self.willAppearIndex];
  402. }
  403. }
  404. if (self.willDisappearIndex == -1) {
  405. self.willDisappearIndex = rightIndex;
  406. [self listWillDisappear:self.willDisappearIndex];
  407. }
  408. }else {
  409. //当前选中的在左边,用户正在从左边往右边滑动
  410. if (ratio > self.initListPercent) {
  411. [self initListIfNeededAtIndex:rightIndex];
  412. }
  413. if (self.willAppearIndex == -1) {
  414. self.willAppearIndex = rightIndex;
  415. if (_validListDict[@(rightIndex)] != nil) {
  416. [self listWillAppear:self.willAppearIndex];
  417. }
  418. }
  419. if (self.willDisappearIndex == -1) {
  420. self.willDisappearIndex = leftIndex;
  421. [self listWillDisappear:self.willDisappearIndex];
  422. }
  423. }
  424. }
  425. - (void)didClickSelectedItemAtIndex:(NSInteger)index {
  426. if (![self checkIndexValid:index]) {
  427. return;
  428. }
  429. self.willAppearIndex = -1;
  430. self.willDisappearIndex = -1;
  431. if (self.currentIndex != index) {
  432. [self listWillDisappear:self.currentIndex];
  433. [self listDidDisappear:self.currentIndex];
  434. [self listWillAppear:index];
  435. [self listDidAppear:index];
  436. }
  437. }
  438. - (void)scrollSelectedItemAtIndex:(NSInteger)index
  439. {
  440. if (![self checkIndexValid:index]) {
  441. return;
  442. }
  443. [self.collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
  444. if (self.isHorizontal) {
  445. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
  446. }else{
  447. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
  448. }
  449. self.currentIndex = index;
  450. [self.collectionView reloadData];
  451. }
  452. - (void)reloadData {
  453. for (id<CGXVerticalMenuListContainerViewDelegate> list in _validListDict.allValues) {
  454. [[list listView] removeFromSuperview];
  455. if ([list isKindOfClass:[UIViewController class]]) {
  456. [(UIViewController *)list removeFromParentViewController];
  457. }
  458. }
  459. [_validListDict removeAllObjects];
  460. [self.collectionView reloadData];
  461. [self listWillAppear:self.currentIndex];
  462. [self listDidAppear:self.currentIndex];
  463. }
  464. @end