// // KBItemListViewController.m // YouHuiProject // // Created by 小花 on 2018/1/26. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBItemListViewController.h" #import "KBGoodCollectionCell.h" #import "KBTypeButtonHeader.h" #import "KBGoodDetailViewController.h" #import "KBGoodListViewController.h" static NSString *cellID = @"KBGoodCollectionCell"; #define KHeaderBarHeight 40 @interface KBItemListViewController () { NSInteger _page; NSInteger _type; BOOL _changeType; } @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *goodsArr; @property (nonatomic, strong) KBTypeButtonHeader *selctionHeader; @end @implementation KBItemListViewController - (void)viewDidLoad { [super viewDidLoad]; [self configParam]; [self configNavigationBar]; [self configCollectionView]; [self loadCategoryGoodsList]; } - (void)configParam { _page = 1; _type = 1; //默认请求推荐的数据 } - (void)configNavigationBar { self.view.backgroundColor = [UIColor whiteColor]; [self.navigationBar setNavTitle:self.model.name]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; [self.navigationBar setShowNavigationBarBottomLine:YES]; } - (void)configCollectionView { self.selctionHeader = [[KBTypeButtonHeader alloc] initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, KHeaderBarHeight)]; self.selctionHeader.backgroundColor = [UIColor whiteColor]; [self.selctionHeader setTypeButtonTitles:@[@"推荐",@"最新",@"销量"] withDownLableHeight:0 andDeleagte:self]; [self.selctionHeader setTypeButtonNormalColor:[UIColor YHColorWithHex:0x666666] andSelectColor:[UIColor YHColorWithHex:0xff2420]]; [self.view addSubview:self.selctionHeader]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; CGFloat width = (SCREEN_WIDTH-5)/2; CGFloat height = width + 102; flowLayout.itemSize = CGSizeMake(width, height); flowLayout.minimumLineSpacing = 5; flowLayout.minimumInteritemSpacing = 0; flowLayout.headerReferenceSize = CGSizeMake(0, 0); self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NavBarHeight+KHeaderBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight) collectionViewLayout:flowLayout]; [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xf6f6f6]; self.collectionView.showsVerticalScrollIndicator = NO; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ [self loadMoreData]; }]; [self.view addSubview: self.collectionView]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } /** 上拉加载 */ - (void)loadMoreData { _page++; _changeType = NO; [self loadCategoryGoodsList]; } /** 加载下部商品列表 */ - (void)loadCategoryGoodsList { NSDictionary *para = @{@"page":@(_page),@"scid":self.model.Id,@"type":@(_type)}; [KBHttp post:CategoryGoods params:para success:^(id json) { if (_changeType) { [self.goodsArr removeAllObjects]; } NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"]]; [self.goodsArr addObjectsFromArray:list]; [self.collectionView reloadData]; [self noMoreDataWithArray:list]; [self.collectionView.mj_footer endRefreshing]; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; }]; } - (void)noMoreDataWithArray:(NSArray *)array { if (array.count <= 0) { MJRefreshBackNormalFooter *foot = (MJRefreshBackNormalFooter *)self.collectionView.mj_footer; [foot setTitle:@"到底啦" forState:MJRefreshStateIdle]; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势 UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer; //获取到拖拽的速度 >0 向下拖动 <0 向上拖动 CGFloat velocity = [pan velocityInView:scrollView].y; if (velocity <- 10) { //向上拖动,隐藏导航栏 if (self.navigationBar.y == -NavBarHeight) { return; } [UIView animateWithDuration:0.35 animations:^{ self.navigationBar.y = -NavBarHeight; self.selctionHeader.y = KStatusBarHeight; self.collectionView.y = KStatusBarHeight + KHeaderBarHeight; }]; }else if (velocity > 10) { //向下拖动,显示导航栏 if (self.navigationBar.y == 0) { return; } [UIView animateWithDuration:0.35 animations:^{ self.navigationBar.y = 0; self.selctionHeader.y = NavBarHeight; self.collectionView.y = NavBarHeight + KHeaderBarHeight; }]; }else if(velocity == 0){ //停止拖拽 } } #pragma mark =============== YHTypeButtonActionDelegate ============= - (void)didClickTypeButtonAction:(UIButton *)button withIndex:(NSInteger)index { _type = index + 1; _changeType = YES; [self loadCategoryGoodsList]; [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES]; } #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.goodsArr.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; KBChildGoodModel *model = self.goodsArr[indexPath.row]; cell.model = model; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.goodsArr[indexPath.row]; if ([model.type isEqualToString:@"1"]) { //专场 KBGoodListViewController *list = [[KBGoodListViewController alloc] init]; list.cate_id = model.goods_id; [self.navigationController pushViewController:list animated:YES]; }else { //详情 KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; [self.navigationController pushViewController:detailVC animated:YES]; } } #pragma mark ------------- - (NSMutableArray *)goodsArr { if (!_goodsArr) { _goodsArr = [NSMutableArray array]; } return _goodsArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end