Why collectionView cell centering works only in one direction?
<p>I'm trying to centerelize my cells on horizontal scroll. I've written one method, but it works only when I scroll to right, on scroll on left it just scrolls, without stopping on the cell's center.</p>

<p>Can anyone help me to define this bug, please?</p>

<pre><code>class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -&gt; 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 (attributes.center.x == 0) || (attributes.center.x &gt; (cv.contentOffset.x + halfWidth) &amp;&amp; velocity.x &lt; 0) {
                    continue
                }

                // == First time in the loop == //
                guard let candAttrs = candidateAttributes else {
                    candidateAttributes = attributes
                    continue
                }

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

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

            if(proposedContentOffset.x == -(cv.contentInset.left)) {
                return proposedContentOffset
            }

             return CGPoint(x: floor(candidateAttributes!.center.x - halfWidth), y: proposedContentOffset.y)
        }
    } else {
        print("else")
    }

    // fallback
    return super.targetContentOffsetForProposedContentOffset(proposedContentOffset)
    }
}
</code></pre>

<p>And in my UIViewController:</p>

<pre><code>override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    var insets = self.collectionView.contentInset
    let value = ((self.view.frame.size.width - ((CGRectGetWidth(collectionView.frame) - 35))) * 0.5)
    insets.left = value
    insets.right = value
    self.collectionView.contentInset = insets

    self.collectionView.decelerationRate = UIScrollViewDecelerationRateNormal
}
</code></pre>

<p>If you have any question - please ask me</p>