SnappingLayout
Description
SnappingLayout enables a snap behaviour for UICollectionViews
.
It works with horizontal scrolling and there are 3 different types: left, center and right.
You can scroll your collection view and it will automatically snap to the chosen position after decelerating.
The snap supports all kinds of collection view configurations: with sectionInset
, minimumLineSpacing
, contentInset
and so forth.
Requirements
iOS 9.0
Swift 4.2
Installation
SnappingLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SnappingLayout'
Example Project
To run the example project, clone the repo, and run pod install from the Example directory first. Open SnappingPresentationViewController to see the code needed to implement the snapping.
Usage
Just use SnappingLayout as a replacement for the traditional UICollectionViewFlowLayout.
It's as simple as:
- Importing it:
import SnappingLayout
- Instantiating it:
let snappingLayout = SnappingLayout()
- And using it:
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: snappingLayout)
- Set the deceleration rate of the collection view to fast:
collectionView.decelerationRate = .fast
There are three types of snappingLayout
: left, center and right.
Left
This is the default value when a new instance of snappingLayout
is created and it will snap the position of the cell to the left side of the collection view.
let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .left
Center
This will snap the position of the cell to the center of the collection view.
let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .center
Right
This will snap the position of the cell to the right of the collection view.
let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .right
Author
Kévin, [email protected]
License
SnappingLayout is available under the MIT license. See the LICENSE file for more info.