UICollectionView監(jiān)聽(tīng)滾動(dòng)距離及獲取當(dāng)前indexPath

前言

由于開(kāi)發(fā)中遇到一個(gè)既有按鈕可以點(diǎn)擊展示上一個(gè)和下一個(gè),也可以手動(dòng)滑動(dòng)展示上一個(gè)下一個(gè),導(dǎo)致監(jiān)聽(tīng)UICollectionView到底展示的是哪個(gè)indexPath比較麻煩,特記錄一下

參考:

監(jiān)聽(tīng)滾動(dòng)停止:

注意:頁(yè)面中點(diǎn)擊上一個(gè)、下一個(gè)按鈕調(diào)用的- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;不會(huì)走這個(gè)滾動(dòng)的回調(diào)

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 停止類(lèi)型1、停止類(lèi)型2
    BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging &&    !scrollView.decelerating;
    if (scrollToScrollStop) {
        [self scrollViewDidEndScroll];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        // 停止類(lèi)型3
        BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (dragToDragStop) {
            [self scrollViewDidEndScroll];
        }
    }
}

#pragma mark - scrollView 停止?jié)L動(dòng)監(jiān)測(cè)
- (void)scrollViewDidEndScroll {
   NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *indexPath = indexPaths.firstObject;
    // 將collectionView在控制器view的中心點(diǎn)轉(zhuǎn)化成collectionView上的坐標(biāo)
//    CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
//    // 獲取這一點(diǎn)的indexPath
//    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pInView];
   

}

調(diào)用UICollectionView的reloadData后立刻調(diào)用scrollToItemAtIndexPath...,則scrollToItemAtIndex不會(huì)起作用,需要在relaodData后延遲一下調(diào)用

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.collectionView registerNib:[UINib nibWithNibName:@"xxxxCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"xxxxCollectionViewCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataList.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(Main_Screen_Width, collectionView.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    xxxxCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"xxxxCollectionViewCell" forIndexPath:indexPath];
    XXXModel *model = self.dataList[indexPath.item];
    cell.model = model;
    return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 停止類(lèi)型1、停止類(lèi)型2
    BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging &&    !scrollView.decelerating;
    if (scrollToScrollStop) {
        [self scrollViewDidEndScroll];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        // 停止類(lèi)型3
        BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (dragToDragStop) {
            [self scrollViewDidEndScroll];
        }
    }
}

#pragma mark - scrollView 停止?jié)L動(dòng)監(jiān)測(cè)
- (void)scrollViewDidEndScroll {
   NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *indexPath = indexPaths.firstObject;
    // 將collectionView在控制器view的中心點(diǎn)轉(zhuǎn)化成collectionView上的坐標(biāo)
//    CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
//    // 獲取這一點(diǎn)的indexPath
//    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pInView];
    self.selectIndex = indexPath.item;
    [self updateShowTitle];
}

- (IBAction)preBtnClicked:(id)sender {
    self.selectIndex -=1;
    [self updateShow];
    
}
- (IBAction)nextBtnClicked:(id)sender {
    self.selectIndex +=1;
    [self updateShow];
}

- (void)setDataList:(NSArray<VIPLevelDataModel *> *)dataList
{
    _dataList = dataList;
    [self initUpdateShow];
}
- (void)updateShowTitle
{
    if(self.dataList.count == 0){
        return;
    }
    if(self.selectIndex == 0){
        self.preBtn.enabled = NO;
    }else{
        self.preBtn.enabled = YES;
    }
    if(self.selectIndex >= self.dataList.count-1){
        self.nextBtn.enabled = NO;
    }else{
        self.nextBtn.enabled = YES;
    }
    NSString *imageName = [NSString stringWithFormat:@"vip_red%ld",self.selectIndex];
    self.vipImage.image = [UIImage imageNamed:imageName];
}
- (void)initUpdateShow
{
    if(self.dataList.count == 0){
        return;
    }
    [self updateShowTitle];
    [self.collectionView reloadData];
    WS(weakSelf)
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self
.selectIndex inSection:0];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    });

}
- (void)updateShow
{
    if( self.dataList.count == 0){
        return;
    }
    [self updateShowTitle];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectIndex inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容