// // KBNineNineScrollChildViewController.m // YouHuiProject // // Created by xiaoxi on 2018/1/29. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBNineNineScrollChildViewController.h" #import "KBCollectionView.h" #import "KBScrollChildCollectionViewCell.h" #import "KBNineNineScrollChannelModel.h" #import "KBNineNineRequestViewModel.h" #import "KBGoodCollectionCell.h" #import "KBCollectionView.h" #import "KBNineCollectionView.h" static NSString *const cellID = @"KBScrollChildCollectionViewCell"; static NSInteger page = 1; @interface KBNineNineScrollChildViewController () @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, strong) UIButton *toTopButton; @end @implementation KBNineNineScrollChildViewController - (void)viewDidLoad { [super viewDidLoad]; [self initNavBar]; [self initSubviews]; [self request]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changedSexRequest) name:ChangeSex object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)initNavBar { self.navigationBar.hidden = YES; } - (void)initSubviews { [self.view addSubview:self.collectionView]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-50, self.collectionView.height-100, 40, 40)]; button.backgroundColor = [UIColor grayColor]; button.layer.cornerRadius = 20; [self.view addSubview:button]; [button addTarget:self action:@selector(scrollToTopAction) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"回顶部" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:12]; self.toTopButton = button; self.toTopButton.alpha = .0; } - (void)scrollToTopAction { [self.collectionView scrollToTop]; } #pragma mark - request - (void)request { [KBNineNineRequestViewModel requestNineNineCategoryGoodsParamPage:page scid:self.model.Id type:@"1" stype:@"1" success:^(NSArray *array) { if (array.count > 0) { [self.dataSource addObjectsFromArray:array]; [self.collectionView reloadData]; } [self.collectionView.mj_footer endRefreshing]; } failure:^(NSError *error) { [self.collectionView.mj_footer endRefreshing]; }]; } - (void)changedSexRequest { [self.dataSource removeAllObjects]; page = 1; [self request]; } #pragma mark - collectionview - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBScrollChildCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; cell.backgroundView.backgroundColor = [UIColor whiteColor]; cell.contentView.backgroundColor = [UIColor whiteColor]; KBChildGoodModel *model = self.dataSource[indexPath.item]; cell.model = model; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeZero; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(yh_NineNineScrollChildViewControllerDidSelectItem:)]) { KBChildGoodModel *model = self.dataSource[indexPath.item]; [self.delegate yh_NineNineScrollChildViewControllerDidSelectItem:model]; } } - (void)setCanScroll:(BOOL)canScroll { _canScroll = canScroll; self.collectionView.canScroll = canScroll; } #pragma mark - scrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (!self.canScroll) { scrollView.contentOffset = CGPointZero; } if (scrollView.contentOffset.y <= 0) { // if (!self.fingerIsTouch) {//这里的作用是在手指离开屏幕后也不让显示主视图,具体可以自己看看效果 // return; // } self.canScroll = NO; scrollView.contentOffset = CGPointZero; [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil];//到顶通知父视图改变状态 } [self setScrollToTopView:scrollView changeHeight:SCREEN_HEIGHT]; } - (void)setScrollToTopView:(UIScrollView *)scrollView changeHeight:(CGFloat)height{ CGFloat offY = scrollView.contentOffset.y; if (offY >= height-1) { UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer; //获取到拖拽的速度 >0 向下拖动 <0 向上拖动 CGFloat velocity = [pan velocityInView:scrollView].y; if (velocity <- 10) { //向上拖动,隐藏 [UIView animateWithDuration:0.35 animations:^{ self.toTopButton.alpha = 0.0; }]; }else if (velocity > 10) { //向下拖动,显示 [UIView animateWithDuration:0.35 animations:^{ self.toTopButton.alpha = 1.0; }]; }else if(velocity == 0){ //停止拖拽 } }else { [UIView animateWithDuration:0.35 animations:^{ self.toTopButton.alpha = 0.0; }]; } } #pragma mark - lazy - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(FITSIZE(184.9), FITSIZE(287)); flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; flowLayout.minimumLineSpacing = FITSIZE(5); flowLayout.minimumInteritemSpacing = FITSIZE(5); _collectionView = [[KBNineCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-NavBarHeight-FITSIZE(40)) collectionViewLayout:flowLayout]; _collectionView.backgroundView.backgroundColor = [UIColor clearColor]; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.bounces = YES; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[KBScrollChildCollectionViewCell class] forCellWithReuseIdentifier:cellID]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } kWeak(self); _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ page ++; [selfWeak request]; }]; } return _collectionView; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } @end