【发布时间】:2015-08-13 00:56:09
【问题描述】:
我正在尝试找出如何找到我的UICollectionView 的最中心单元格,该单元格设置为仅水平滚动。
我试过这个 SO:how to get indexPath for cell which is located in the center of UICollectionView 但它没有成功,我想是因为我正在使用我的 UICollectionView 的 contentInset 属性。
我正在做的是将流布局的 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