123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- //
- // KBChildPageViewController.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/16.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "KBChildPageViewController.h"
- #import "KBChildPageViewController.h"
- #import "KBChildHeaderView.h"
- #import "KBTypeButtonHeader.h"
- #import "XLPlainFlowLayout.h"
- #import "KBHeaderReusableView.h"
- #import "KBGoodCollectionCell.h"
- #import "KBChildCategoryModel.h"
- #import "KBChildGoodModel.h"
- #import "KBItemListViewController.h"
- #import "KBGoodListViewController.h"
- #import "KBGoodDetailViewController.h"
- #import "SelectView.h"
- #import "KBOtherFilterView.h"
- #import "KBTodayModelView.h"
- #import "KBClassifyListController.h"
- #import "KBOtherTopCollectionCell.h"
- #import "AdJumpManager.h"
- #import "KBChildPageAdView.h"
- static NSString *headerID = @"headerID";
- static NSString *cellID = @"KBGoodCollectionCell";
- static NSString *KSectionHeader = @"SectionHeader";
- static NSString *KLDOtherTopCollectionCell = @"KBOtherTopCollectionCell";
- static CGFloat selectedViewHeight = 40.f;
- @interface KBChildPageViewController ()
- <
- UICollectionViewDelegate,
- UICollectionViewDataSource,
- XHCollectionViewDelegateFlowLayout,
- ClassifySelectViewDelegate,
- YHChildHeaderViewDelegate
- >
- {
- NSInteger _page;
- NSInteger _type;
- NSInteger _is_has_coupon;
- BOOL _changeType;
- UIView *topRecomView;
- }
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) NSArray *categoryArr;
- @property (nonatomic, strong) NSMutableArray *goodsArr;
- @property (nonatomic, strong) NSArray *adDataArr;
- @property (nonatomic, strong) KBOtherFilterView *selectedView;
- @property (nonatomic, strong) NSArray *filtrateArray;
- @property (nonatomic, strong) KBTodayModelView *recommonView;
- @property (nonatomic, strong) KBChildHeaderView *channelHeader;
- @property (nonatomic, strong) KBChildPageAdView *headAdView;
- @property (nonatomic, strong) KBOtherTopCollectionCell *topCell;
- @end
- @implementation KBChildPageViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self configParam];
- [self configCollectionView];
- [self requesFiltrateParam];
- [self loadChannelHeader];
- [self loadAdData];
- }
- - (void)configParam {
- _page = 0;
- _type = 1; //默认请求推荐的数据
- }
- - (void)configCollectionView {
-
- [self.collectionView removeFromSuperview];
- XLPlainFlowLayout *flowLayout = [[XLPlainFlowLayout alloc]init];
- flowLayout.naviHeight = 0;
- flowLayout.minimumLineSpacing = 5;
- flowLayout.minimumInteritemSpacing = 0;
- flowLayout.headerReferenceSize = CGSizeMake(0, 0);
- self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
- [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
- [self.collectionView registerClass:[KBOtherTopCollectionCell class] forCellWithReuseIdentifier:KLDOtherTopCollectionCell];
- [self.collectionView registerClass:[KBHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
- [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeader];
-
- self.collectionView.backgroundColor = [UIColor clearColor];
- self.collectionView.showsVerticalScrollIndicator = NO;
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
- [self refreshData];
- }];
- header.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhite;
- header.stateLabel.textColor=[UIColor whiteColor];
- header.lastUpdatedTimeLabel.hidden=YES;
- [header setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
- // header.lastUpdatedTimeLabel.hidden = YES;
- // header.stateLabel.hidden = YES;
-
- //正在刷新的图片
- // NSArray *imgArr = @[[UIImage imageNamed:@"ant1"],[UIImage imageNamed:@"ant2"]];
- // [header setImages:imgArr duration:0.3 forState:MJRefreshStateRefreshing];
- // [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStateWillRefresh];
- // [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStatePulling];
- self.collectionView.mj_header = header;
-
- self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
- [self loadMoreData];
- }];
- [self.collectionView.mj_footer beginRefreshing];
- [self.view addSubview: self.collectionView];
-
- [self configCollectionHeader];
- }
- - (void)configCollectionHeader {
- UIView *recomView = [[UIView alloc] initWithFrame:CGRectMake(Fitsize(10), 0, SCREEN_WIDTH-Fitsize(20), Fitsize(208))];
- topRecomView = recomView;
- recomView.layer.cornerRadius = Fitsize(8);
- recomView.backgroundColor = [UIColor whiteColor];
- UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, Fitsize(65), Fitsize(40))];
- title.font = [UIFont systemFontOfSize:Fitsize(14)];//[UIColor homeRedColor]
- title.centerX = SCREEN_WIDTH/2;
- title.text = @"热门商品";
- [title sizeToFit];
- title.textColor =[UIColor gradientWidthFromColor:[UIColor YHColorWithHex:0x53F3FD] toColor:[UIColor YHColorWithHex:0x726BFF] withWidth:title.width];
- [recomView addSubview:title];
- UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(title.left-20, 0, Fitsize(13), Fitsize(15))];
- icon.centerY = title.centerY;
- icon.image = [UIImage imageNamed:@"hotRecom_left"];
- [recomView addSubview:icon];
- UIImageView *icon2 = [[UIImageView alloc] initWithFrame:CGRectMake(title.right+5, 0, Fitsize(13), Fitsize(15))];
- icon2.centerY = title.centerY;
- icon2.image = [UIImage imageNamed:@"hotRecom_right"];
- [recomView addSubview:icon2];
-
- KBTodayModelView *modelView = [[KBTodayModelView alloc] initWithFrame:CGRectMake(Fitsize(0), Fitsize(40), SCREEN_WIDTH-Fitsize(20), Fitsize(168)) titleNumOfLine:1];
- modelView.isOhterPage = YES;
- modelView.name = self.model.name;
- modelView.Id = self.model.Id;
- self.recommonView = modelView;
- [recomView addSubview:modelView];
-
- self.headAdView = [[KBChildPageAdView alloc] initWithFrame:CGRectMake(Fitsize(10), 0, SCREEN_WIDTH-Fitsize(20), 0)];
- __weak typeof(self) weakSelf = self;
- self.headAdView.tapAdBlock = ^(AdPopModel * _Nonnull model) {
- if (model) {
- [AdJumpManager jumpToPageWithModel:model ParentViewControll:weakSelf];
- }
-
- };
-
- self.channelHeader = [[KBChildHeaderView alloc] initWithFrame:CGRectMake(Fitsize(10), 0, SCREEN_WIDTH-Fitsize(20), 0) delegete:self];
- }
- #pragma mark ====================== Load Data ==========
- - (void)loadAdData {
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/adv/advByType",BaseURL];
- NSDictionary *para = @{@"adv_type":@"7",
- @"category_id":self.model.Id};
- [KBHttp get:url params:para success:^(id json) {
-
- self.adDataArr = [NSArray yy_modelArrayWithClass:[AdPopModel class] json:json[@"data"]];
- if (self.adDataArr.count > 0) {
- //展示广告
- self.headAdView.height = Fitsize(111);
- self.headAdView.y = self.channelHeader.height;
- topRecomView.y = self.headAdView.bottom+5;
- AdPopModel *model = self.adDataArr.firstObject;
- [self.headAdView setAdModel:model];
- }else {
- //隐藏广告
- self.headAdView.height = 0;
- topRecomView.y = self.channelHeader.height;
- }
- [self.collectionView reloadData];
- } failure:^(NSError *error) {
-
- }];
- }
- /**
- 加载筛选范围
- */
- - (void)requesFiltrateParam {
- NSString *urlString = [NSString stringWithFormat:@"%@/api/categoryNew/priceSection", BaseURL];
- [KBHttp post:urlString params:nil success:^(id json) {
- self.filtrateArray = json[@"data"];
- // [self.view addSubview:self.selectedView];
-
- } failure:^(NSError *error) {
-
- }];
- }
- /**
- 加载下部商品列表
- */
- - (void)loadCategoryGoodsList:(BOOL)refresh switchButton:(UIButton *)switchButton{
-
-
- NSDictionary *param = @{@"page":@(_page),
- @"category_id":self.model.Id,
- @"sort":@(_type),
- @"is_has_coupon":@(_is_has_coupon),
- @"stype":@"0"
- };
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/stockTopCategoryList",BaseURL];
- [KBHttp post:url params:param success:^(id json) {
-
- if (refresh) [self.goodsArr removeAllObjects];
- if ([self.collectionView.mj_header isRefreshing])[self.goodsArr removeAllObjects];
-
- NSArray *topList = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"][@"top"]];
- [self.recommonView setGoodData:topList];
-
- NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"][@"list"]];
- [self.goodsArr addObjectsFromArray:list];
- [self noMoreDataWithArray:list];
- [self.collectionView reloadData];
-
- [self.collectionView.mj_header endRefreshing];
- [self.collectionView.mj_footer endRefreshing];
- [SVProgressHUD dismiss];
- switchButton.selected = !switchButton.selected;
-
-
- } failure:^(NSError *error) {
- [self.collectionView.mj_header endRefreshing];
- [self.collectionView.mj_footer endRefreshing];
- [SVProgressHUD dismiss];
- }];
- }
- - (void)noMoreDataWithArray:(NSArray *)array {
- if (array > 0) {
- self.collectionView.footRefreshState = MJFooterRefreshStateLoadMore;
- }else {
- self.collectionView.footRefreshState = MJFooterRefreshStateNoMore;
- }
- }
- /**
- 加载头部分类
- */
- - (void)loadChannelHeader {
- NSString *url = [NSString stringWithFormat:@"%@/api/v2/channel/getSonGroup",BaseURL];
- NSDictionary *para = @{@"category_id":self.model.Id};
- [KBHttp post:url params:para success:^(id json) {
-
- NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildCategoryModel class] json:json[@"data"]];
- [self.channelHeader setChannelDataArr:list];
- if (self.adDataArr.count > 0) {
- self.headAdView.y = self.channelHeader.bottom;
- topRecomView.y = self.headAdView.bottom+5;
- }else {
- topRecomView.y = self.channelHeader.height;
- }
-
- [self.collectionView reloadData];
- [self loadCategoryGoodsList:NO switchButton:nil];
-
-
- } failure:^(NSError *error) {
- [self.collectionView.mj_header endRefreshing];
- [self.collectionView.mj_footer endRefreshing];
- }];
- }
- /**
- 上拉加载
- */
- - (void)loadMoreData {
- _page++;
- [self loadCategoryGoodsList:NO switchButton:nil];
- }
- - (void)refreshData {
- _page = 1;
- [self loadChannelHeader];
- // [self loadCategoryGoodsList:YES switchButton:nil];
- }
- #pragma mark ------ YHChildHeaderViewDelegate --------
- - (void)YHChildHeaderViewDidSelectedIndex:(NSInteger)index model:(KBChildCategoryModel *)model {
- KBClassifyListController *list = [[KBClassifyListController alloc] init];
- list.firstCategoryId = self.model.Id;
- list.cate_id = model.Id;
- list.name = model.name;
- list.isOtherPage = YES;
- [self.navigationController pushViewController:list animated:YES];
-
- NSString *tag = [NSString stringWithFormat:@"%@-%@",self.model.name,model.name];
- [MobClick event:CategoryClassify label:tag];
- }
- #pragma mark -------JC_SelectViewDelegate
- ////仅显示优惠券
- //- (void)ClassifyselectItme:(KBOtherFilterView *)selectView onlyTicketClick:(UIButton *)switchBtn {
- // [SVProgressHUD show];
- // [self.collectionView scrollToTop];
- // _is_has_coupon = switchBtn.selected ? 0 : 1;
- // [self loadCategoryGoodsList:YES switchButton:switchBtn];
- //}
- //上方条件选择
- - (void)ClassifyselectTopButton:(KBOtherFilterView *)selectView withIndex:(NSInteger)index withButtonType:(JCButtonClickType )type1 button:(UIButton *)button{
- [SVProgressHUD show];
- //价格
- if (index == 2&&type1) {
- switch (type1) {
- case JCButtonClickTypeNormal:
- //正常价格
- {
- NSLog(@"上边按钮的正常价格");
- }
- break;
- case JCButtonClickTypeUp:
- //价格升序排列
- {
- NSLog(@"上边按钮的价格升序排列");
- _type = 3;
- }
- break;
- case JCButtonClickTypeDown:
- //价格降序排列
- {
- NSLog(@"上边按钮的价格降序排列");
- _type = 4;
- }
- break;
- default:
- break;
- }
- }else if (index == 0){//综合
-
- NSLog(@"上边按钮的综合");
- _type = 1;
-
- }else if (index == 1){//销量
-
- NSLog(@"上边按钮的销量");
- _type = 2;
-
- }else{//筛选
- _is_has_coupon = button.selected ? 0 : 1;
- [self loadCategoryGoodsList:YES switchButton:button];
- return;
- NSLog(@"上边有券按钮");
- }
-
- [self loadCategoryGoodsList:YES switchButton:nil];
- }
- #pragma mark ------
- #pragma mark - scrollView
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
- UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
- //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
- CGFloat velocity = [pan velocityInView:scrollView].y;
- if (velocity <- 10) {
- //向上拖动,隐藏导航栏
-
- self.collectionView.height = ChildTableViewHeight+NavBarHeight-KStatusBarHeight;
- }else if (velocity > 10) {
- //向下拖动,显示导航栏
-
- self.collectionView.height = ChildTableViewHeight;
- }else if(velocity == 0){
- //停止拖拽
- }
-
- NSNotification *noti = [[NSNotification alloc] initWithName:ScrollDirectorNoti object:nil userInfo:@{@"director":@(velocity)}];
- [[NSNotificationCenter defaultCenter] postNotification:noti];
- }
- #pragma mark ============ UICollectionView Delegate && DataSource ==========
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 2;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- if (section == 0) {
- return 1;
- }else {
- return self.goodsArr.count;
- }
-
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- if (indexPath.section == 0) {
-
- CGFloat height = topRecomView.bottom+5;
- return CGSizeMake(SCREEN_WIDTH, height);
- }else {
- CGFloat width = (SCREEN_WIDTH)/2;
- CGFloat height = width + 102;
- return CGSizeMake(width-FITSIZE(12.5f), height);
- }
-
-
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
-
- if (indexPath.section == 0) {
- self.topCell = [collectionView dequeueReusableCellWithReuseIdentifier:KLDOtherTopCollectionCell forIndexPath:indexPath];
- [self.topCell addSubview:self.channelHeader];
- [self.topCell addSubview:self.headAdView];
- [self.topCell addSubview:topRecomView];
- return self.topCell;
- }else {
- KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
- KBChildGoodModel *model = self.goodsArr[indexPath.row];
- cell.model = model;
- return cell;
- }
-
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
- UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeader forIndexPath:indexPath];
- [header addSubview:self.selectedView];
- return header;
- }
- - (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 *detail = [[KBGoodDetailViewController alloc] init];
- DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
- detail.requestModel = requestModel;
-
- KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.model.Id source:homeAction];
- detail.eventModel = evevtModel;
-
- [self.navigationController pushViewController:detail animated:YES];
-
- }
-
- [MobClick event:category_goods label:self.model.name];
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
- if (section == 0) {
- return CGSizeMake(0, 0);
- }else {
- return CGSizeMake(SCREEN_WIDTH, selectedViewHeight+5);
- }
-
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
- if (section == 0) {
- return UIEdgeInsetsMake(0, 0, 0, 0);
- }else{
- return UIEdgeInsetsMake(0, FITSIZE(10), 0, FITSIZE(10));
- }
- }
- #pragma mark - JHCollectionViewDelegateFlowLayout
- - (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout backgroundColorForSection:(NSInteger)section
- {
- return [@[
- [UIColor clearColor],
- [UIColor YHColorWithHex:0xf6f6f6],
- [UIColor yellowColor]
- ] objectAtIndex:section];
- }
- #pragma mark -------------
- - (NSMutableArray *)goodsArr {
- if (!_goodsArr) {
- _goodsArr = [NSMutableArray array];
- }
- return _goodsArr;
- }
- - (KBOtherFilterView *)selectedView {
- if (!_selectedView) {
- _selectedView = [[KBOtherFilterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, selectedViewHeight) withArr:self.filtrateArray];
- _selectedView.delegate = self;
- }
- return _selectedView;
- }
- - (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
|