2016-08-11 16 views
4

私はcollectionViewを使用してカルーセルのような効果を実装しようとしています。私はUICollectionViewを実装し、セルを横に表示するように設定しましたUICollectionView - 一度に1つのセルで横方向のページング

唯一の問題は、1つのセルだけを表示し、そのセルを画面の中央に配置する方法です。

注:ページングは​​有効です。

私は、このコードを見つけ、それを試してみましたが、悲しいことに

コードの参照動作しませんでした:コード上の中央のセルについて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

は右であり、また、これらの2つの方法を実装:

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); 
    } 
関連する問題