口袋优选

KBItemListViewController.m 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // KBItemListViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/26.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBItemListViewController.h"
  9. #import "KBGoodCollectionCell.h"
  10. #import "KBTypeButtonHeader.h"
  11. #import "KBGoodDetailViewController.h"
  12. #import "KBGoodListViewController.h"
  13. static NSString *cellID = @"KBGoodCollectionCell";
  14. #define KHeaderBarHeight 40
  15. @interface KBItemListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,YHTypeButtonActionDelegate,UICollectionViewDelegateFlowLayout>
  16. {
  17. NSInteger _page;
  18. NSInteger _type;
  19. BOOL _changeType;
  20. }
  21. @property (nonatomic, strong) UICollectionView *collectionView;
  22. @property (nonatomic, strong) NSMutableArray *goodsArr;
  23. @property (nonatomic, strong) KBTypeButtonHeader *selctionHeader;
  24. @end
  25. @implementation KBItemListViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. [self configParam];
  29. [self configNavigationBar];
  30. [self configCollectionView];
  31. [self loadCategoryGoodsList];
  32. }
  33. - (void)configParam {
  34. _page = 1;
  35. _type = 1; //默认请求推荐的数据
  36. }
  37. - (void)configNavigationBar {
  38. self.view.backgroundColor = [UIColor whiteColor];
  39. [self.navigationBar setNavTitle:self.model.name];
  40. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  41. [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  42. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  43. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  44. [self.navigationBar setShowNavigationBarBottomLine:YES];
  45. }
  46. - (void)configCollectionView {
  47. self.selctionHeader = [[KBTypeButtonHeader alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, KHeaderBarHeight)];
  48. self.selctionHeader.backgroundColor = [UIColor whiteColor];
  49. [self.selctionHeader setTypeButtonTitles:@[@"推荐",@"最新",@"销量"] withDownLableHeight:0 andDeleagte:self];
  50. [self.selctionHeader setTypeButtonNormalColor:[UIColor YHColorWithHex:0x666666] andSelectColor:[UIColor YHColorWithHex:0xff2420]];
  51. [self.view addSubview:self.selctionHeader];
  52. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  53. CGFloat width = (SCREEN_WIDTH-5)/2;
  54. CGFloat height = width + 102;
  55. flowLayout.itemSize = CGSizeMake(width, height);
  56. flowLayout.minimumLineSpacing = 5;
  57. flowLayout.minimumInteritemSpacing = 0;
  58. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  59. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight+KHeaderBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout];
  60. [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  61. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6];
  62. self.collectionView.showsVerticalScrollIndicator = NO;
  63. self.collectionView.delegate = self;
  64. self.collectionView.dataSource = self;
  65. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  66. [self loadMoreData];
  67. }];
  68. [self.view addSubview: self.collectionView];
  69. }
  70. - (void)backAction {
  71. [self.navigationController popViewControllerAnimated:YES];
  72. }
  73. /**
  74. 上拉加载
  75. */
  76. - (void)loadMoreData {
  77. _page++;
  78. _changeType = NO;
  79. [self loadCategoryGoodsList];
  80. }
  81. /**
  82. 加载下部商品列表
  83. */
  84. - (void)loadCategoryGoodsList {
  85. NSDictionary *para = @{@"page":@(_page),@"scid":self.model.Id,@"type":@(_type)};
  86. [KBHttp post:CategoryGoods params:para success:^(id json) {
  87. if (_changeType) {
  88. [self.goodsArr removeAllObjects];
  89. }
  90. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]];
  91. [self.goodsArr addObjectsFromArray:list];
  92. [self.collectionView reloadData];
  93. [self noMoreDataWithArray:list];
  94. [self.collectionView.mj_footer endRefreshing];
  95. } failure:^(NSError *error) {
  96. [self.collectionView.mj_footer endRefreshing];
  97. }];
  98. }
  99. - (void)noMoreDataWithArray:(NSArray *)array {
  100. if (array.count <= 0) {
  101. MJRefreshBackNormalFooter *foot = (MJRefreshBackNormalFooter *)self.collectionView.mj_footer;
  102. [foot setTitle:@"到底啦" forState:MJRefreshStateIdle];
  103. }
  104. }
  105. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  106. {
  107. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  108. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  109. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  110. CGFloat velocity = [pan velocityInView:scrollView].y;
  111. if (velocity <- 10) {
  112. //向上拖动,隐藏导航栏
  113. if (self.navigationBar.y == -NavBarHeight) {
  114. return;
  115. }
  116. [UIView animateWithDuration:0.35 animations:^{
  117. self.navigationBar.y = -NavBarHeight;
  118. self.selctionHeader.y = KStatusBarHeight;
  119. self.collectionView.y = KStatusBarHeight + KHeaderBarHeight;
  120. }];
  121. }else if (velocity > 10) {
  122. //向下拖动,显示导航栏
  123. if (self.navigationBar.y == 0) {
  124. return;
  125. }
  126. [UIView animateWithDuration:0.35 animations:^{
  127. self.navigationBar.y = 0;
  128. self.selctionHeader.y = NavBarHeight;
  129. self.collectionView.y = NavBarHeight + KHeaderBarHeight;
  130. }];
  131. }else if(velocity == 0){
  132. //停止拖拽
  133. }
  134. }
  135. #pragma mark =============== YHTypeButtonActionDelegate =============
  136. - (void)didClickTypeButtonAction:(UIButton *)button withIndex:(NSInteger)index {
  137. _type = index + 1;
  138. _changeType = YES;
  139. [self loadCategoryGoodsList];
  140. [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
  141. }
  142. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  143. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  144. {
  145. return self.goodsArr.count;
  146. }
  147. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  148. {
  149. return CGSizeMake(0, 0);
  150. }
  151. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  152. {
  153. return CGSizeMake(0, 0);
  154. }
  155. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  158. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  159. cell.model = model;
  160. return cell;
  161. }
  162. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  163. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  164. if ([model.type isEqualToString:@"1"]) {
  165. //专场
  166. KBGoodListViewController *list = [[KBGoodListViewController alloc] init];
  167. list.cate_id = model.goods_id;
  168. [self.navigationController pushViewController:list animated:YES];
  169. }else {
  170. //详情
  171. KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init];
  172. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  173. detailVC.requestModel = requestModel;
  174. [self.navigationController pushViewController:detailVC animated:YES];
  175. }
  176. }
  177. #pragma mark -------------
  178. - (NSMutableArray *)goodsArr {
  179. if (!_goodsArr) {
  180. _goodsArr = [NSMutableArray array];
  181. }
  182. return _goodsArr;
  183. }
  184. - (void)didReceiveMemoryWarning {
  185. [super didReceiveMemoryWarning];
  186. // Dispose of any resources that can be recreated.
  187. }
  188. /*
  189. #pragma mark - Navigation
  190. // In a storyboard-based application, you will often want to do a little preparation before navigation
  191. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  192. // Get the new view controller using [segue destinationViewController].
  193. // Pass the selected object to the new view controller.
  194. }
  195. */
  196. @end