HDCollectionView+MultipleScroll.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // HDCollectionView+MultipleScroll.m
  3. // HDCollectionView_Example
  4. //
  5. // Created by HaoDong chen on 2019/5/19.
  6. // Copyright © 2019 donggelaile. All rights reserved.
  7. //
  8. #import "HDCollectionView+MultipleScroll.h"
  9. #import "HDMultipleScrollListView.h"
  10. #import <objc/runtime.h>
  11. @implementation HDCollectionView(MultipleScroll)
  12. - (void)setCurrentSubSc:(UIScrollView *)currentSubSc
  13. {
  14. objc_setAssociatedObject(self, &HDMUltipleCurrentSubScrollKey, currentSubSc, OBJC_ASSOCIATION_ASSIGN);
  15. }
  16. - (UIScrollView *)currentSubSc
  17. {
  18. return objc_getAssociatedObject(self, &HDMUltipleCurrentSubScrollKey);
  19. }
  20. - (void)hd_autoDealScrollViewDidScrollEvent:(UIView*)subScrollContentView topH:(CGFloat)topH
  21. {
  22. if (!subScrollContentView) {
  23. return;
  24. }
  25. __weak typeof(self) weakS = self;
  26. __weak typeof(subScrollContentView) weakContentV = subScrollContentView;
  27. [self hd_setScrollViewDidScrollCallback:^(UIScrollView *scrollView) {
  28. if ((NSInteger)scrollView.contentInset.top == HDMainDefaultTopEdge) {
  29. CGFloat fitY = MAX(0, weakS.collectionV.contentOffset.y);
  30. scrollView.contentOffset = CGPointMake(0, fitY);
  31. }
  32. UIScrollView *subSc = weakS.currentSubSc;
  33. if (subSc.contentOffset.y>0) {
  34. scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, topH);
  35. }
  36. if (weakS.collectionV.contentOffset.y < topH) {
  37. [weakS dealAllSubScrollViewScrollEnabled:weakContentV];
  38. }
  39. }];
  40. }
  41. - (void)dealAllSubScrollViewScrollEnabled:(UIView*)scrollContent
  42. {
  43. //层次遍历sc
  44. NSMutableArray *queue = scrollContent.subviews.mutableCopy;
  45. while (queue.count) {
  46. UIView *firstView = [queue firstObject];
  47. //出队
  48. [queue removeObjectAtIndex:0];
  49. //子view入队
  50. [queue addObjectsFromArray:firstView.subviews];
  51. if ([firstView isKindOfClass:[UICollectionView class]] || [firstView isKindOfClass:[UITableView class]]){
  52. UIScrollView *sc = (UIScrollView *)firstView;
  53. if (sc.contentSize.height>sc.frame.size.height) {
  54. sc.contentOffset = CGPointMake(0, -sc.contentInset.top);
  55. }
  56. }
  57. }
  58. }
  59. /*
  60. // Only override drawRect: if you perform custom drawing.
  61. // An empty implementation adversely affects performance during animation.
  62. - (void)drawRect:(CGRect)rect {
  63. // Drawing code
  64. }
  65. */
  66. @end