2015-05-30 35 views
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)); 
} 

回答

0

我想通了這一點使用幫助從這個回答我: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; 
      }); 
     } 
    } 
} 
相關問題