2016-07-29 32 views
2

我有一個collectionView,並且它的scrollview有一個擴展名,它可以幫助我像這樣獲得我的單元格20像素(來自下一個單元格的最小空間)右側:每次滾動視圖結束時居中顯示一個collectionView的單元格

enter image description here

現在我想改變我的擴展,該中心以這樣的容器: enter image description here

我的分機是這樣的:(如果我可以與任何其他的方式做到隨時告訴它)

extension MyProject : UIScrollViewDelegate 
{ 
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
    { 
     let layout = self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout 
     let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing 

     var offset = targetContentOffset.memory 
     let index = (offset.x + scrollView.contentInset.left)/cellWidthIncludingSpacing 
     let roundedIndex = round(index) 

     offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top) 
     targetContentOffset.memory = offset 
    } 
} 
+0

是否啓用了的CollectionView滾動分頁考慮contentInset? –

+0

@BharatModi沒有任何變化 –

回答

2

我想說

offset = CGPoint(
    x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left + cellWithIncludingSpacing/2 - scrollView.bounds.size.width/2, 
    y: -scrollView.contentInset.top 
) 

,或者更好地

let visibleWidth = scrollView.bounds.size.width 
    - scrollView.contentInset.left 
    - scrollView.contentInset.right 
offset = CGPoint(
    x: roundedIndex * cellWidthIncludingSpacing 
    - scrollView.contentInset.left 
    + layout.itemSize.width/2 
    - visibleWidth/2, 
    y: -scrollView.contentInset.top 
) 
+0

我會盡力測試它,直到星期一我會通知你謝謝 –

+0

謝謝我今天試了一下,它的工作!它有左多一點像素,但它可能是任何括號導致這個問題? –

+0

更可能來自正確的內容插入沒有被納入公式。 – PierreMB

相關問題