/// 这里是水平滚动的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];
}
网友评论