2016-08-11 89 views
4

我試圖使用collectionView實現類似輪播的輪播。我實施了UICollectionView並將其設置爲水平顯示單元格UICollectionView - 一次水平分頁與一個單元格

唯一的問題是,我如何只允許一個單元格的外觀並將該單元格居中在屏幕上。

注意:啓用尋呼功能。

我發現這個代碼,嘗試過,但遺憾的是沒有工作

碼參考:Create a Paging UICollectionView with Swift

override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { 

     if let cv = self.collectionView { 

      let cvBounds = cv.bounds 
      let halfWidth = cvBounds.size.width * 0.5; 
      let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth; 

      if let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds) { 

       var candidateAttributes : UICollectionViewLayoutAttributes? 
       for attributes in attributesForVisibleCells { 

        // == Skip comparison with non-cell items (headers and footers) == // 
        if attributes.representedElementCategory != UICollectionElementCategory.Cell { 
         continue 
        } 

        if let candAttrs = candidateAttributes { 

         let a = attributes.center.x - proposedContentOffsetCenterX 
         let b = candAttrs.center.x - proposedContentOffsetCenterX 

         if fabsf(Float(a)) < fabsf(Float(b)) { 
          candidateAttributes = attributes; 
         } 

        } 
        else { // == First time in the loop == // 

         candidateAttributes = attributes; 
         continue; 
        } 


       } 

       return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y); 

      } 

     } 

     // Fallback 
     return super.targetContentOffsetForProposedContentOffset(proposedContentOffset) 
    } 

回答

3

爲中心的細胞上面的代碼是正確的,也是實現這兩個方法:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 
    let currentIndex:CGFloat = self.GridCollectionView.contentOffset.x/self.GridCollectionView.frame.size.width 
    pageControl.currentPage = Int(currentIndex) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {    
    return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.width+40) 
} 
+1

當單元格寬度與collectionView寬度相同時,您的答案完美工作,但否則當單元格的寬度與collectionView單元格 –

+0

不同時,會在設置故事板中的單元格寬度時出現問題。 –

+0

我做過,第一個和最後一個單元格沒有居中的原因是什麼? –

0

如果商品的寬度是動態的,您可以嘗試下面的代碼

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { 
     CGFloat offsetAdjustment = MAXFLOAT; 
     CGFloat proposedHorizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds)/2.0); 
     CGRect targetRect = CGRectMake(proposedContentOffset.x, proposedContentOffset.y, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height); 
     NSArray* array = [self layoutAttributesForElementsInRect:targetRect]; 

     for (UICollectionViewLayoutAttributes* layoutAttributes in array) { 
      CGFloat itemHorizontalCenter = layoutAttributes.center.x; 
      if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) { 
       offsetAdjustment = itemHorizontalCenter - horizontalCenter; 
      } 
     } 
     return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y); 
    } 
相關問題