SMPager or SimplePager is a lightweight, memory-efficient implementation of UIScrollView written in Swift. It works with reusable views the same way as UIKit's UITableView does.
- Renders any type of UIView. (UIImageView, UILabel, UIViewController view, etc.)
- Uses the least amount of memory required to render the views.
- Supports infinite scrolling.
- Populates your UIViews using delegation.
SMPager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SMPager', '~> 0.1'
In your ViewController initialize and add the pager to the superview.
import SMPager
class ViewController {
let imageURLs = [
"https://picsum.photos/id/177/500/900",
"https://picsum.photos/id/886/500/900",
"https://picsum.photos/id/362/500/900",
"https://picsum.photos/id/569/500/900",
]
lazy var pager: SMPager = {
let pager = SMPager()
pager.pagerDataSource = self
pager.pagerDelegate = self
pager.infiniteScrollingEnabled = true
return pager
}()
func setupConstraints() {
pager.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
pager.leftAnchor.constraint(equalTo: self.view.leftAnchor),
pager.rightAnchor.constraint(equalTo: self.view.rightAnchor),
pager.topAnchor.constraint(equalTo: navBar.bottomAnchor),
pager.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
}
// MARK: View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(pager)
setupConstraints()
}
}
Implement the SMPagerDelegate and SMPagerDataSource
// MARK: SMPagerDelegate
extension ViewController: SMPagerDelegate {
func pageChanged(page: Int) {
pageControl.currentPage = page
}
}
// MARK: SMPagerDataSource
extension ViewController: SMPagerDataSource {
func numberOfViews() -> Int {
return imageURLs.count
}
func viewForIndex(_ index: Int, reusedView: UIView?) -> UIView {
let imageView: UIImageView
if let reuseView = reusedView as? UIImageView {
imageView = reuseView
} else {
imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
}
if let imageUrl = URL(string: imageURLs[index]) {
imageView.kf.setImage(with: imageUrl)
}
return imageView
}
}
// Enables/disables the infinite scrolling mode. You can set this variable anytime without the need to call reloadData(). Default value is false.
var infiniteScrollingEnabled: Bool
// Set the SMPagerDelegate. (see bellow)
var pagerDelegate: SMPagerDelegate?
// Set the SMPagerDataSource. (see bellow)
var pagerDataSource: SMPagerDataSource?
// Get the visible page index (readonly)
var currentIndex: Int
// Moves to previous or next page. You can pass a boolean value if you want the transition between pages to be animated (default value is true).
func moveToPreviousPage(animated: Bool = true)
func moveToNextPage(animated: Bool = true)
// Moves to a specific page without animation.
func move(to page: Int)
// Reloads all the pager views.
func reloadData()
// Called when a page is changed.
func pageChanged(page: Int)
// Return the number of views to be rendered.
func numberOfViews() -> Int
// Return the view to be rendered for a specific index. reusedView is passed if it's available.
func viewForIndex(_ index: Int, reusedView: UIView?) -> UIView
Bill Panagiotopoulos, billp.dev@gmail.com
SMPager is available under the MIT license. See the LICENSE file for more info.