天天省钱快报

KBNineNineScrollChildViewController.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // KBNineNineScrollChildViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/1/29.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBNineNineScrollChildViewController.h"
  9. #import "KBCollectionView.h"
  10. #import "KBScrollChildCollectionViewCell.h"
  11. #import "KBNineNineScrollChannelModel.h"
  12. #import "KBNineNineRequestViewModel.h"
  13. #import "KBGoodCollectionCell.h"
  14. #import "KBCollectionView.h"
  15. #import "KBNineCollectionView.h"
  16. static NSString *const cellID = @"KBScrollChildCollectionViewCell";
  17. static NSInteger page = 1;
  18. @interface KBNineNineScrollChildViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  19. @property (nonatomic, strong) NSMutableArray *dataSource;
  20. @property (nonatomic, strong) UIButton *toTopButton;
  21. @end
  22. @implementation KBNineNineScrollChildViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. [self initNavBar];
  26. [self initSubviews];
  27. [self request];
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changedSexRequest) name:ChangeSex object:nil];
  29. }
  30. - (void)didReceiveMemoryWarning {
  31. [super didReceiveMemoryWarning];
  32. }
  33. - (void)initNavBar {
  34. self.navigationBar.hidden = YES;
  35. }
  36. - (void)initSubviews {
  37. [self.view addSubview:self.collectionView];
  38. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-50, self.collectionView.height-100, 40, 40)];
  39. button.backgroundColor = [UIColor grayColor];
  40. button.layer.cornerRadius = 20;
  41. [self.view addSubview:button];
  42. [button addTarget:self action:@selector(scrollToTopAction) forControlEvents:UIControlEventTouchUpInside];
  43. [button setTitle:@"回顶部" forState:UIControlStateNormal];
  44. button.titleLabel.font = [UIFont systemFontOfSize:12];
  45. self.toTopButton = button;
  46. self.toTopButton.alpha = .0;
  47. }
  48. - (void)scrollToTopAction {
  49. [self.collectionView scrollToTop];
  50. }
  51. #pragma mark - request
  52. - (void)request {
  53. [KBNineNineRequestViewModel requestNineNineCategoryGoodsParamPage:page scid:self.model.Id type:@"1" stype:@"1" success:^(NSArray *array) {
  54. if (array.count > 0) {
  55. [self.dataSource addObjectsFromArray:array];
  56. [self.collectionView reloadData];
  57. }
  58. [self.collectionView.mj_footer endRefreshing];
  59. } failure:^(NSError *error) {
  60. [self.collectionView.mj_footer endRefreshing];
  61. }];
  62. }
  63. - (void)changedSexRequest {
  64. [self.dataSource removeAllObjects];
  65. page = 1;
  66. [self request];
  67. }
  68. #pragma mark - collectionview
  69. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  70. return 1;
  71. }
  72. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  73. return self.dataSource.count;
  74. }
  75. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  76. KBScrollChildCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  77. cell.backgroundColor = [UIColor whiteColor];
  78. cell.backgroundView.backgroundColor = [UIColor whiteColor];
  79. cell.contentView.backgroundColor = [UIColor whiteColor];
  80. KBChildGoodModel *model = self.dataSource[indexPath.item];
  81. cell.model = model;
  82. return cell;
  83. }
  84. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  85. return CGSizeZero;
  86. }
  87. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  88. if ([self.delegate respondsToSelector:@selector(yh_NineNineScrollChildViewControllerDidSelectItem:)]) {
  89. KBChildGoodModel *model = self.dataSource[indexPath.item];
  90. [self.delegate yh_NineNineScrollChildViewControllerDidSelectItem:model];
  91. }
  92. }
  93. - (void)setCanScroll:(BOOL)canScroll {
  94. _canScroll = canScroll;
  95. self.collectionView.canScroll = canScroll;
  96. }
  97. #pragma mark - scrollView
  98. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  99. {
  100. if (!self.canScroll) {
  101. scrollView.contentOffset = CGPointZero;
  102. }
  103. if (scrollView.contentOffset.y <= 0) {
  104. // if (!self.fingerIsTouch) {//这里的作用是在手指离开屏幕后也不让显示主视图,具体可以自己看看效果
  105. // return;
  106. // }
  107. self.canScroll = NO;
  108. scrollView.contentOffset = CGPointZero;
  109. [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil];//到顶通知父视图改变状态
  110. }
  111. [self setScrollToTopView:scrollView changeHeight:SCREEN_HEIGHT];
  112. }
  113. - (void)setScrollToTopView:(UIScrollView *)scrollView changeHeight:(CGFloat)height{
  114. CGFloat offY = scrollView.contentOffset.y;
  115. if (offY >= height-1) {
  116. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  117. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  118. CGFloat velocity = [pan velocityInView:scrollView].y;
  119. if (velocity <- 10) {
  120. //向上拖动,隐藏
  121. [UIView animateWithDuration:0.35 animations:^{
  122. self.toTopButton.alpha = 0.0;
  123. }];
  124. }else if (velocity > 10) {
  125. //向下拖动,显示
  126. [UIView animateWithDuration:0.35 animations:^{
  127. self.toTopButton.alpha = 1.0;
  128. }];
  129. }else if(velocity == 0){
  130. //停止拖拽
  131. }
  132. }else {
  133. [UIView animateWithDuration:0.35 animations:^{
  134. self.toTopButton.alpha = 0.0;
  135. }];
  136. }
  137. }
  138. #pragma mark - lazy
  139. - (UICollectionView *)collectionView {
  140. if (!_collectionView) {
  141. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  142. flowLayout.itemSize = CGSizeMake(FITSIZE(184.9), FITSIZE(287));
  143. flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  144. flowLayout.minimumLineSpacing = FITSIZE(5);
  145. flowLayout.minimumInteritemSpacing = FITSIZE(5);
  146. _collectionView = [[KBNineCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-NavBarHeight-FITSIZE(40)) collectionViewLayout:flowLayout];
  147. _collectionView.backgroundView.backgroundColor = [UIColor clearColor];
  148. _collectionView.backgroundColor = [UIColor clearColor];
  149. _collectionView.bounces = YES;
  150. _collectionView.delegate = self;
  151. _collectionView.dataSource = self;
  152. _collectionView.backgroundColor = [UIColor clearColor];
  153. [_collectionView registerClass:[KBScrollChildCollectionViewCell class] forCellWithReuseIdentifier:cellID];
  154. if (@available(iOS 11.0, *)) {
  155. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  156. }
  157. kWeak(self);
  158. _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  159. page ++;
  160. [selfWeak request];
  161. }];
  162. }
  163. return _collectionView;
  164. }
  165. - (NSMutableArray *)dataSource {
  166. if (!_dataSource) {
  167. _dataSource = [NSMutableArray array];
  168. }
  169. return _dataSource;
  170. }
  171. @end