I'm trying to implement the UICollectionView
for custom cells in the shape of circle. Now by default the circles are aligned in the same way as the normal square cell: top circle and bottom circle are on the same vertical line. How can I change the alignment to this: the top circle and two circles below it form an equilateral triangle (positions of top circle and bottom circle are shifted by radius' length)? As below:
from OOO
OOO
OOO
to O O O
O O O (no spacing among the circles)
O O O
The basic idea is to create a custom
UICollectionViewLayout
that implements:collectionViewContentSize
, i.e., what's the size of the full, scrollablecontentSize
of the collection view;layoutAttributesForItem(at indexPath:)
, i.e., what are the key attributes (namelycenter
andsize
) of a particular cell; andlayoutAttributesForElements(in rect:)
, i.e., what are the key attributes for cells that fall within this particularrect
... this will be used to identify which cells are visible at any given point in time as well as the attributes for those cells; this is basically an array of the attributes for the cells from the previous method, filtered down to just the ones within thatrect
.Thus, in Swift 3 you could do something like:
That yields:
Clearly, customize this as you see fit, but it illustrates the basic idea.
In my original answer, below, I assumed you wanted to see these cells in a circle, as shown in WWDC 2012 video Advanced Collection Views and Building Custom Layouts (about 40+ minutes into the video). See that below.
For example, in Swift 3:
Then you can simply set the
collectionViewLayout
and then implement the standardUICollectionViewDataSource
methods.That yields:
See https://github.com/robertmryan/CircularCollectionView for sample.
Note, you mentioned that you want "no spacing among the circles", so just adjust the
radius
and/oritemSize
accordingly to get the layout you want.