【问题标题】:UICollectionView find center cell when content inset is usedUICollectionView 在使用内容插入时找到中心单元格
【发布时间】:2015-08-13 00:56:09
【问题描述】:

我正在尝试找出如何找到我的UICollectionView 的最中心单元格,该单元格设置为仅水平滚动。

我试过这个 SO: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));
}

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    我自己通过这个答案的帮助想出了这个:https://stackoverflow.com/a/22696037/1533438

    最终代码:

    - (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;
                });
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多