title: swift positionate center UICollectionViewCell
body: <p>I am trying to implement the "<strong>snap to effect</strong>" but couldn't get it working, is there something that I'm doing wrong?</p>
  
  <p>Subclassing <code>UICollectionViewFlowLayout</code> and overriding <code>targetContentOffsetForProposedContentOffset</code> the function:</p>
  
  <pre><code>override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -&gt; CGPoint {
  
      var offsetAdjustment:CGFloat = CGFloat(MAXFLOAT)
      let verticalCenter:CGFloat = proposedContentOffset.y + (CGRectGetHeight(self.collectionView!.bounds) / 2.0)
      let proposedRect:CGRect = CGRectMake(proposedContentOffset.x,0.0,self.collectionView!.bounds.size.width,self.collectionView!.bounds.size.height)
  
      let array:NSArray = self.layoutAttributesForElementsInRect(proposedRect)!
  
      for layoutAttributes : AnyObject in array {
  
          if let _layoutAttributes = layoutAttributes as? UICollectionViewLayoutAttributes {
  
              if _layoutAttributes.representedElementCategory != UICollectionElementCategory.Cell {
                  continue
              }
  
              let itemVerticalCenter:CGFloat = layoutAttributes.center.y
  
              let _verticalCenter = fabsf(Float(itemVerticalCenter) - Float(verticalCenter))
              let _offsetAdjustment = fabsf(Float(offsetAdjustment))
  
              if (_verticalCenter &lt; _offsetAdjustment) {
                  offsetAdjustment = itemVerticalCenter - verticalCenter
              }
          }
      }
      return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y)
  }
  </code></pre>
  
  <p><a href="https://www.dropbox.com/sh/zrs7bchgqxsm9cm/AADvI48kskuG9RVRDSMH3frga?dl=0">Sample project here</a> and <a href="https://www.dropbox.com/s/ry03ggp8jm87a25/Cards.png?dl=0">snap effect, desired output</a></p>