UICollectionView 在使用内容插入时找到中心单元格

UICollectionView find center cell when content inset is used

我想弄清楚如何找到我的 UICollectionView 的最中心单元格,该单元格设置为仅水平滚动。

我试过这个所以:how to get indexPath for cell which is located in the center of UICollectionView 但它没有用,我想是因为我正在使用 UICollectionViewcontentInset 属性。

我正在做的是将流布局的 contentInset 左右设置为 self.view 边界宽度的 1/2 减去 itemSize 的 1/2宽度。因此,我不确定如何计算最中心的单元格。如果能提供任何帮助,我将不胜感激。代码:

CGPoint cellCenter = CGPointMake((self.collectionView.center.x + 
self.collectionView.contentOffset.x) - 
(self.sampleFlowLayout.sectionInset.left + 
self.sampleFlowLayout.sectionInset.right), self.collectionView.center.y);

NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:cellCenter];

if (indexPath)
{
    NSInteger tag = [self.collectionView cellForItemAtIndexPath:indexPath].tag;

    if (tag != self.currentSample)
    {
        self.currentSample = tag;

        self.imageView.image = [self sampleImageWithOption:self.currentSample];
    }
}

- (void)viewDidLoad
{
    self.sampleFlowLayout = [[UICollectionViewFlowLayout alloc]init];

    CGSize itemSize = CGSizeMake(63, 63);   

    self.sampleFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    self.sampleFlowLayout.sectionInset = UIEdgeInsetsMake(0, (self.view.bounds.size.width / 2) - (itemSize.width / 2), 0, (self.view.bounds.size.width / 2) - (itemSize.width / 2));
}

我借助这个答案的帮助自己解决了这个问题:

最终代码:

- (void)snapCollectionView:(UICollectionView *)collectionView withProposedOffset:(CGPoint)proposedOffset
{
    CGRect cvBounds = collectionView.bounds;
    CGFloat halfWidth = cvBounds.size.width * 0.5;
    CGFloat proposedContentOffsetCenterX = proposedOffset.x + halfWidth;

    NSArray *attributesArray = [collectionView.collectionViewLayout layoutAttributesForElementsInRect:cvBounds];

    UICollectionViewLayoutAttributes *candidateAttributes;

    for (UICollectionViewLayoutAttributes *attributes in attributesArray)
    {
        if (attributes.representedElementCategory !=
            UICollectionElementCategoryCell)
        {
            continue;
        }

        if (!candidateAttributes)
        {
            candidateAttributes = attributes;
            continue;
        }

        if (fabsf(attributes.center.x - proposedContentOffsetCenterX) <
            fabsf(candidateAttributes.center.x - proposedContentOffsetCenterX))
        {
            candidateAttributes = attributes;
        }
    }

    CGPoint offset = CGPointMake(candidateAttributes.center.x - halfWidth, proposedOffset.y);
    [collectionView setContentOffset:offset animated:YES];

    CGPoint cellCenter = CGPointMake(offset.x + self.sampleCollectionView.bounds.size.width / 2.0, self.sampleCollectionView.bounds.size.height / 2.0);

    NSIndexPath *indexPath = [self.sampleCollectionView indexPathForItemAtPoint:cellCenter];

    if (indexPath)
    {
        NSInteger tag = [self.sampleCollectionView cellForItemAtIndexPath:indexPath].tag;

        if (tag != self.currentSample)
        {
            self.currentSample = tag;

            UIImage *image;

            if (self.currentSample == 0)
            {
                image = self.image;
            }
            else
            {
                image = [self sampleImageWithOption:self.currentSample];
            }

            dispatch_async(dispatch_get_main_queue(),
            ^{
                self.imageView.image = image;
            });
        }
    }
}