You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6 KiB

  1. //
  2. // PagedView.swift
  3. // GME Remit
  4. //
  5. // Created by Armaan Shrestha on 21/08/2022.
  6. // Copyright © 2022 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. protocol PagedViewDelegate: AnyObject {
  10. func didMoveToPage(index: Int)
  11. }
  12. class PagedView: UIView, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
  13. // MARK: - Initialization
  14. init(pages: [UIView] = []) {
  15. self.pages = pages
  16. super.init(frame: .zero)
  17. self.setupUI()
  18. }
  19. required init?(coder: NSCoder) {
  20. fatalError("init(coder:) has not been implemented")
  21. }
  22. // MARK: - Properties
  23. public weak var delegate: PagedViewDelegate?
  24. public var pages: [UIView] {
  25. didSet {
  26. self.collectionView.reloadData()
  27. }
  28. }
  29. private lazy var collectionView: UICollectionView = {
  30. let layout = UICollectionViewFlowLayout()
  31. layout.scrollDirection = .horizontal
  32. let collectionView = UICollectionView(
  33. frame: .zero,
  34. collectionViewLayout: layout
  35. )
  36. collectionView.showsHorizontalScrollIndicator = false
  37. collectionView.isPagingEnabled = true
  38. collectionView.register(PageCollectionViewCell.self, forCellWithReuseIdentifier: "PageCollectionViewCell")
  39. collectionView.delegate = self
  40. collectionView.dataSource = self
  41. collectionView.translatesAutoresizingMaskIntoConstraints = false
  42. return collectionView
  43. }()
  44. // MARK: - UI Setup
  45. func setupUI() {
  46. self.translatesAutoresizingMaskIntoConstraints = false
  47. self.addSubview(collectionView)
  48. collectionView.backgroundColor = .white
  49. NSLayoutConstraint.activate([
  50. collectionView.widthAnchor
  51. .constraint(equalTo: self.widthAnchor),
  52. collectionView.heightAnchor
  53. .constraint(equalTo: self.heightAnchor),
  54. collectionView.centerXAnchor
  55. .constraint(equalTo: self.centerXAnchor),
  56. collectionView.centerYAnchor
  57. .constraint(equalTo: self.centerYAnchor)
  58. ])
  59. }
  60. // MARK: - Data Source
  61. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  62. return pages.count
  63. }
  64. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  65. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PageCollectionViewCell", for: indexPath) as! PageCollectionViewCell
  66. let page = self.pages[indexPath.item]
  67. cell.view = page
  68. return cell
  69. }
  70. // MARK: - Actions
  71. public func moveToPage(at index: Int) {
  72. self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
  73. }
  74. // MARK: - Delegate
  75. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  76. let page = Int(self.collectionView.contentOffset.x / self.collectionView.frame.size.width)
  77. self.delegate?.didMoveToPage(index: page)
  78. }
  79. // MARK: - Layout Delegate
  80. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  81. return CGSize(width: self.collectionView.frame.width,
  82. height: self.collectionView.frame.height)
  83. }
  84. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  85. return 0
  86. }
  87. }