Last active
August 6, 2018 23:11
-
-
Save marcosatanaka/c034761abb1873b6d2dc30710058d5a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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