CollectionViewSlantedLayout
Slanted Layout is a subclass of the UICollectionViewLayout allowing the display of slanted cells in a UICollectionView, either horizontally or vertically.
Features
- Pure Swift 5.
- Works with every UICollectionView.
- Horizontal and vertical scrolling support.
- Dynamic cells height
- Fully Configurable
Installation
CocoaPods
CollectionViewSlantedLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:
1 |
pod 'CollectionViewSlantedLayout', '~> 3.1' |
Usage
- Import
CollectionViewSlantedLayout
module to your controller
1import CollectionViewSlantedLayout - Create an instance and add it to your
UICollectionView
.
1let slantedSayout = CollectionViewSlantedLayout() UICollectionView(frame: .zero, collectionViewLayout: slantedSayout) - Use the
CollectionViewSlantedCell
class either your cells or subclass it.
Properties
- slantingSize:
1@IBInspectable var slantingSize: UInt
The slanting size. The default value of this property is either 75 or 80. - slantingDirection:
1var slantingDirection: SlantingDirection
The slanting direction. The default value of this property isupward
. - slantingAngle:
1fileprivate(set) var slantingAngle: CGFloat
The angle, in radians, of the slanting. The value of this property could be used to apply a rotation transform on the cell’s contentView in thecollectionView(_:cellForItemAt:)
method implementation, either horizontally or vertically.
1if let layout = collectionView.collectionViewLayout as? CollectionViewSlantedLayout { cell.contentView.transform = CGAffineTransform(rotationAngle: layout.rotationAngle) } - scrollDirection:
1var scrollDirection: UICollectionViewScrollDirection
In the scroll direction of the grid. The grid layout scrolls along one axis only, either horizontally or vertically. The default value of this property isvertical
- isFirstCellExcluded:
1@IBInspectable var isFirstCellExcluded: Bool
Set it totrue
to disable the slanting for the first cell. The default value of this property isfalse
. - isLastCellExcluded:
1@IBInspectable var isLastCellExcluded: Bool
Set it totrue
to disable the slanting for the last cell. The default value of this property isfalse
. - lineSpacing:
1<span class="pl-k">@IBInspectable</span> <span class="pl-k">var</span> lineSpacing<span class="pl-k">:</span> CGFloat
The spacing to use between two items. The default value of this property is
10.0
. - itemSize:
1@IBInspectable var itemSize: CGFloat
The default size to use for cells. If the delegate does not implement the
collectionView(_:layout:sizeForItemAt:)
method, the slanted layout uses the value in this property to set the size of each cell. This results in cells that all have the same size. The default value of this property is225
. - zIndexOrder:
1var zIndexOrder: ZIndexOrder
The zIndex order of the items in the layout. The default value of this property is
ascending
.
The grid layout scrolls along one axis only, either horizontally or vertically
Protocols
The CollectionViewDelegateSlantedLayout
protocol defines methods that let you coordinate with a CollectionViewSlantedLayout
object to implement a slanted layout. The CollectionViewDelegateSlantedLayout
protocol has the following methods:
1 |
optional func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: CollectionViewSlantedLayout, sizeForItemAt indexPath: IndexPath) -> CGFloat |
This method asks the delegate for the size of the specified item’s cell, either horizontally or vertically
If you do not implement this method, the slanted layout uses the values in its itemSize
property to set the size of items instead.
Your implementation of this method can return either a fixed set of sizes or dynamically adjust the sizes based on the cell’s content, either horizontally or vertically
For more blogs please click here.