SSPager
Super Simple Pager
Example
To run the example project, clone the repo, and run pod install
from the SSPagerExample directory first.
Installation
CocoaPods
SSPager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SSPager', '~> 1.0.0'
Swift Package Manager
Create a Package.swift
file.
import PackageDescription
let package = Package(
name: "SSPager",
dependencies: [
.package(url: "https://github.com/9oya/SSPager.git", from: "1.0.0")
],
targets: [
.target(
name: "SSPager",
path: "SSPager"
)
]
)
Usage
@IBOutlet weak var pagerView: SSPagerView!
pagerView.dataSource = self
pagerView.delegate = self
pagerView.register(SSPagerViewCell.self, forCellWithReuseIdentifier: "SSPagerViewCell"))
DataSource
func numberOfPages(_ pagerView: SSPagerView) -> Int
func pagerView(_ pagerView: SSPagerView, cellForItemAt index: Int) -> SSPagerViewCell
Paging Option
pagerView.interitemSpacing = 20.0
pagerView.isInfinite = true
pagerView.automaticSlidingInterval = 2.0
pagerView.pagingMode = .oneStep // .scrollable .disable
pagerView.itemSize = CGSize(width: cellWidth, // view.frame.width * 0.7
height: cellHeight) // view.frame.height * 0.7
pagerView.contentsInset = UIEdgeInsets(top: 100,
left: (view.bounds.width - cellWidth) / 2,
bottom: 100,
right: (view.bounds.width - cellWidth) / 2)
Control
func scrollToPage(at index: Int, animated: Bool)
Delegate
func pagerViewDidSelectPage(at index: Int)
func pagerViewWillEndDragging(_ scrollView: UIScrollView, targetIndex: Int)
Rx
SSPager provide a subspec SSPager/Rx
pod 'SSPager/Rx', '~> 1.0.0'
@IBOutlet weak var pagerView: SSPagerView!
/*
You don't need to set delegate and datasource
`pagerView.dataSource = self`
`pagerView.delegate = self`
*/
pagerView.register(SSPagerViewCell.self, forCellWithReuseIdentifier: "SSPagerViewCell")
Binding:
// let itemColors = [UIColor.red, UIColor.orange, UIColor.yellow, UIColor.green, UIColor.blue]
Observable.just(itemColors)
.bind(to: pagerView.rx.pages(cellIdentifier: defaultCellId))
{ idx, color, cell in
cell.backgroundColor = color
}
.disposed(by: disposeBag)
pagerView.rx.pageSelected
.bind(onNext: { idx in
print("Page \(idx) is selected.")
})
.disposed(by: disposeBag)
Author
Eido Goya, [email protected]
License
SSPager is available under the MIT license. See the LICENSE file for more info.