TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2017 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Raffaele Cerullo.
STWCollectionView is an UICollectionView sublcass easy to create and manage for a collection with carousel effect and is very simple to customize.
You can set how many cells are visible simultaneously or give a fixed size to cells, and the collection will set automatically the simultaneously visible cells.
You can set the space between the cells and the vertical and horizontal padding than you can see a bit of the adjacent cells.
Further, STWCollectionView has a delegate to better manage cells during the scrolling phase such as: the cell's currently visible indexPaths and their percentage of positioning based on the center.
CocoaPods:
pod 'STWCollectionView'
Set as you would an UICollectionView
let stwCollectionView = STWCollectionView()
override func viewDidLoad() {
super.viewDidLoad()
// delegate & data source
stwCollectionView.delegate = self
stwCollectionView.dataSource = self
// layout subviews
...
// register collection cells
stwCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentifier)
}
// datasource extensions
...
In addition to the UICollectionViewDelegate
methods, STWCollectionView
has three other methods to manage scrolling thanks to STWCollectionViewDelegate
that is a UICollectionViewDelegate
protocol subclass
extension ViewController: STWCollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Scroll to selected cell
stwCollectionView.scrollTo(indexPath: indexPath, animated: true)
}
func collectionViewDidEndDeceleratingWithPercentages(_ collectionView: STWCollectionView, visibleIndexPaths indexPaths: [IndexPath], percentageVisibleIndexPaths percentages: [CGFloat]) {
print("indexPaths after Decelerating: \(indexPaths)")
print("percentages after Decelerating: \(percentages)")
}
func collectionViewDidEndScrollingAnimationWithPercentages(_ collectionView: STWCollectionView, visibleIndexPaths indexPaths: [IndexPath], percentageVisibleIndexPaths percentages: [CGFloat]) {
print("indexPaths after Scrolling Animation: \(indexPaths)")
print("percentages after Scrolling Animation: \(percentages)")
}
func collectionViewDidScrollWithPercentages(_ collectionView: STWCollectionView, visibleIndexPaths indexPaths: [IndexPath], percentageVisibleIndexPaths percentages: [CGFloat]) {
print("indexPaths during Scrolling: \(indexPaths)")
print("percentages during Scrolling: \(percentages)")
}
}
You can customize STWCollectionView
for all your needs
STWCollectionView setting properties:
verticalPadding
Distance of cells form top and bottom border.
var verticalPadding: CGFloat { get set }
// default: 20
horizontalPadding
Distance of cells form left and right border.
var horizontalPadding: CGFloat { get set }
// default: 20
itemSpacing
Distance beetween cells.
var itemSpacing: CGFloat { get set }
// default: 20
fixedCellsNumber
Numbers of simultaneous visible cells.
var fixedCellsNumber: Int { get set }
// default: 1
fixedCellSize
Fixed size of cells.
var fixedCellSize: CGSize? { get set }
forceCentered
Force the contentInset of STWCollectionView so that first and last items are centered.
var forceCentered: Bool { get set }
// default: false
direction
Direction of scrolling.
(supports vertical)
var direction: UICollectionViewScrollDirection { get set }
// default: .horizontal
STWCollectionView getting properties:
currentVisibleIndexPaths
Current visible items' indexPaths.
var currentVisibleIndexPaths: [IndexPath] { get }
currentPage
Current Index in center.
var currentPage: CGFloat { get }
STWCollectionView public methods:
scrollTo(indexPath: animated:)
Scrolls STWCollectionView at specific indexPath.
func scrollTo(indexPath:IndexPath, animated:Bool)
This pod requires a deployment target of iOS 9.0 or greater
@Steewitter, [email protected]
STWCollectionView is available under the MIT license. See the LICENSE file for more info.