0
我想弄清楚如何找到我的UICollectionView
的中心大多數單元格,它只能水平滾動。當使用內容插入時,UICollectionView找到中心單元格
我試過這個SO:how to get indexPath for cell which is located in the center of UICollectionView但它沒有工作,我想因爲我使用屬性我的UICollectionView
。
我在做的是將流佈局左右兩側的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));
}