czl0325 / ZLCollectionView

为应对类似淘宝首页,京东首页,国美首页等复杂布局而写的Collectionview。基于UICollectionView实现,目前支持标签布局,列布局,百分比布局,定位布局,填充式布局,瀑布流布局等。支持纵向布局和横向布局,可以根据不同的section设置不同的布局,支持拖动cell,头部悬浮,设置section背景色和自定义section背景view,向自定义背景view传递自定义方法。功能强大,超过Android的recyclerview,实现了电影选座等高难度的布局。
MIT License
1.16k stars 166 forks source link

无法监听到滚动到第几 section 值 #46

Closed zeqinjie closed 3 years ago

zeqinjie commented 3 years ago

业务中需要监听滚动时候的section 从而设置头部导航栏 title 需求, 使用ZL 不行,但使用系统的layout可以监听到

参考 https://www.cnblogs.com/qqcc1388/p/5891665.html 方法 ~


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSInteger currentIndex = 0;
CGRect visibleRect = (CGRect){.origin = self.collectionViewLabel.contentOffset, .size = self.collectionViewLabel.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionViewLabel indexPathForItemAtPoint:visiblePoint];
if (currentIndex == visibleIndexPath.section || visibleIndexPath == nil) {
    return;
}
currentIndex = visibleIndexPath.section;
NSLog(@"currentIndex: %ld",currentIndex);

}


> 这样的方式也不行
czl0325 commented 3 years ago
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger currentIndex = 0;
    ZLCollectionViewVerticalLayout* layout = (ZLCollectionViewVerticalLayout*)self.collectionViewLabel.collectionViewLayout;
    NSMutableArray* arr = [NSMutableArray new];
    [arr addObject:@"0"];
    [arr addObjectsFromArray:layout.collectionHeightsArray];
    for (NSInteger i = 0; i < arr.count-1; i++) {
        CGFloat height1 = [arr[i] floatValue];
        CGFloat height2 = [arr[i+1] floatValue];
        if (scrollView.contentOffset.y >= height1 && scrollView.contentOffset.y < height2) {
            currentIndex = i;
            break;
        }
    }
    NSLog(@"当前所在section=%zd", currentIndex);
}