1

I want to build an collection view like this one: Collection View

which has bigger cell at the center and cell is snapped to center of container view, but with Swift 3. I don't want to use library since I want to learn how to build a custom Collection View like this.

I've searched over SO but not found any appropriate solution yet

Community
  • 1
  • 1
Hoang Trung
  • 263
  • 4
  • 15

2 Answers2

1

write that function

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
    }

make collection view [scroll Direction] Horizontal

and [scrolling] tick scrolling enable and paging enable

make cell biger

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let offSet = self.collectionView.contentOffset
    let width = self.collectionView.bounds.size.width

    let index = round(offSet.x / width)
    let newPoint = CGPoint(x: index * size.width, y: offSet.y)


    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in

    },completion: {(UIVIewTransitionCoordinatorContext) in
        self.collectionView.reloadData()
        self.collectionView.setContentOffset(newPoint, animated: true)
    })

}
Sagar Bhut
  • 657
  • 6
  • 28
0

To achieve this you will need to subclass UICollectionViewFlowLayout and override:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

then call super.layoutAt... and alter the cell it returns via its .transform attribute and return your altered attributes

Here is an example I made previously.

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

var att = super.layoutAttributesForElements(in: rect)!

if !(delegate?.reelPlayerFlowIsPreviewMode() ?? true) {
    return att
}
let region = CGRect(x: (self.collectionView?.contentOffset.x)!,
                    y: (self.collectionView?.contentOffset.y)!,
                    width: (self.collectionView?.bounds.size.width)!,
                    height: (self.collectionView?.bounds.size.height)!)

let center = CGPoint(x: region.midX, y: region.midY)

for theCell in att {

    print("\(theCell.indexPath.item)\n\(theCell)\n")
    let cell = theCell.copy() as! UICollectionViewLayoutAttributes
    var f = cell.frame
    let cellCenter = CGPoint(x: f.midX, y: f.midY)
    let realDistance = min(center.x - cellCenter.x, region.width)
    let distance = abs(realDistance)
    let d = (region.width - distance) / region.width
    let p = (max(d, ReelPlayerFlowLayout.minPercent) * ReelPlayerFlowLayout.maxPercent)

    f.origin.x += (realDistance * ((1 - ReelPlayerFlowLayout.maxPercent) + (ReelPlayerFlowLayout.maxPercent - ReelPlayerFlowLayout.minPercent)))

    cell.frame = f
    cell.size = CGSize (width: f.width * p, height: f.height * p)            
    let index = att.index(of: theCell)!
    att[index] = cell
}

return att

}

Sean Lintern
  • 3,103
  • 22
  • 28