Skip to content

Instantly share code, notes, and snippets.

@marcosatanaka
Last active August 6, 2018 23:11
Show Gist options
  • Save marcosatanaka/c034761abb1873b6d2dc30710058d5a8 to your computer and use it in GitHub Desktop.
Save marcosatanaka/c034761abb1873b6d2dc30710058d5a8 to your computer and use it in GitHub Desktop.
import UIKit
class OnboardingCollectionViewController: UICollectionViewController {
private let pages = OnboardingModelFactory.getPages()
private lazy var pageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.numberOfPages = pages.count
return pageControl
}()
override func viewDidLoad() {
super.viewDidLoad()
addPageControlToView()
}
private func addPageControlToView() {
view.addSubview(pageControl)
pageControl.translatesAutoresizingMaskIntoConstraints = false
pageControl.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8).isActive = true
pageControl.heightAnchor.constraint(equalToConstant: 40).isActive = true
pageControl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
}
extension OnboardingCollectionViewController {
// MARK: UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OnboardingCollectionViewCell.reuseIdentifier, for: indexPath)
as? OnboardingCollectionViewCell else { fatalError("Could not cast dequeued cell") }
cell.fill(with: pages[indexPath.row])
return cell
}
}
extension OnboardingCollectionViewController: UICollectionViewDelegateFlowLayout {
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)
}
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageNumber = Int(targetContentOffset.pointee.x / view.frame.width)
pageControl.currentPage = pageNumber
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment