0

horizontalで作成し、pagingモードを有効にしました。今、私は、次の/前のスライドの端を見る能力を持つ画面のそれらの細胞のすべてを作りたいと思う。ここでセンターセルをページングモードにする

は私UICollectionViewです:あなたが唯一の最初に表示することができますし、最後の項目は、中央にある通り

enter image description here

ここ
let collectionViewCardsSlider: UICollectionView = { 

     let collectionMargin = CGFloat(16) 
     let itemSpacing = CGFloat(10) 
     let itemHeight = CGFloat(168) 
     var itemWidth = UIScreen.main.bounds.width - collectionMargin * 2.0 

     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = .horizontal 
     layout.minimumLineSpacing = itemSpacing 
     layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
     layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 
     layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0) 
     layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0) 

     let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     collectionView.isPagingEnabled = true 
     collectionView.showsHorizontalScrollIndicator = false 
     collectionView.decelerationRate = UIScrollViewDecelerationRateFast 
     collectionView.backgroundColor = .clear 
     return collectionView 
    }() 

プレビューです。しかし、私が欲しいものは、このようなものです: enter image description here

どのように私はこれを達成することができますし、また私のコードで何が間違っているのですか?

ありがとうございます。

+0

私はチュートリアル[ここ](http://blog.karmadust.com/centered-を見つけましたページング - プレビュー - セル - オン - ユーコンコレクションビュー/)はまだ試していません。 – chengsam

+0

@chengsam私は自分のコードに実装する方法を理解できませんでした! – Sajad

+0

私はちょうどチュートリアルで遊んだので、この機能を実装するのは簡単です。どのような問題がありますか? – chengsam

答えて

1

はFlowLayoutのを無効ページングのクラスを変更してみてください:

let collectionViewCardsSlider: UICollectionView = { 

    let collectionMargin = CGFloat(16) 
    let itemSpacing = CGFloat(10) 
    let itemHeight = CGFloat(168) 
    var itemWidth = UIScreen.main.bounds.width - collectionMargin * 2.0 

    let layout = CenterCellCollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 
    layout.minimumLineSpacing = itemSpacing 
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
    layout.itemSize = CGSize(width: itemWidth, height: itemHeight) 
    layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0) 
    layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0) 

    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    collectionView.isPagingEnabled = false 
    collectionView.showsHorizontalScrollIndicator = false 
    collectionView.decelerationRate = UIScrollViewDecelerationRateFast 
    collectionView.backgroundColor = .clear 
    return collectionView 
}() 

そして:

class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout { 

    var mostRecentOffset : CGPoint = CGPoint() 

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

     if velocity.x == 0 { 
      return mostRecentOffset 
     } 

     if let cv = self.collectionView { 

      let cvBounds = cv.bounds 
      let halfWidth = cvBounds.size.width * 0.5; 


      if let attributesForVisibleCells = self.layoutAttributesForElements(in: cvBounds) { 

       var candidateAttributes : UICollectionViewLayoutAttributes? 
       for attributes in attributesForVisibleCells { 

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

        if (attributes.center.x == 0) || (attributes.center.x > (cv.contentOffset.x + halfWidth) && velocity.x < 0) { 
         continue 
        } 
        candidateAttributes = attributes 
       } 

       // Beautification step , I don't know why it works! 
       if(proposedContentOffset.x == -(cv.contentInset.left)) { 
        return proposedContentOffset 
       } 

       guard let _ = candidateAttributes else { 
        return mostRecentOffset 
       } 
       mostRecentOffset = CGPoint(x: floor(candidateAttributes!.center.x - halfWidth), y: proposedContentOffset.y) 
       return mostRecentOffset 

      } 
     } 

     // fallback 
     mostRecentOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset) 
     return mostRecentOffset 
    } 


} 
関連する問題