美文网首页
collectionView

collectionView

作者: 林希品 | 来源:发表于2022-01-19 17:05 被阅读0次
    /// 这里是水平滚动的colletionView ,所以 使用了x
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if (scrollView == self.collectionView ) {
            if (!(scrollView.isTracking || scrollView.isDecelerating) || scrollView != self.collectionView) {
                //不是用户滚动的,比如setContentOffset等方法,引起的滚动不需要处理。
                return;
            }
            //用户滚动的才处理
            NSInteger index=scrollView.contentOffset.y;
            CGRect visibleRect = (CGRect){.origin = scrollView.contentOffset, .size = scrollView.bounds.size};
            UICollectionViewLayoutAttributes *topAttributes = [self.collectionView.collectionViewLayout layoutAttributesForElementsInRect:visibleRect].firstObject;
            NSLog(@"visibleIndexPath  =  %ld",index);
            NSLog(@"topAttributes.section  =  %ld",topAttributes.indexPath.section);
        }
    }
    
    //调用此方法可滚动到组头 collectionView
    -(void) scrollToSectionHeaderWithCollectionView:(UICollectionView *)collectionView section:(NSInteger)section {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
        UICollectionViewLayoutAttributes *attribs =
        [collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
        CGPoint topOfHeader = CGPointMake(0, attribs.frame.origin.y - collectionView.contentInset.top);
        [collectionView setContentOffset:topOfHeader animated:YES];
    }
    

    相关文章

      网友评论

          本文标题:collectionView

          本文链接:https://www.haomeiwen.com/subject/nvgrhrtx.html