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.

80 lines
2.2 KiB

  1. //
  2. // ViewPager.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. class ViewPager: UIView {
  10. // MARK: - Initialization
  11. init(tabSizeConfiguration: TabbedView.SizeConfiguration) {
  12. self.sizeConfiguration = tabSizeConfiguration
  13. super.init(frame: .zero)
  14. self.setupUI()
  15. tabbedView.delegate = self
  16. pagedView.delegate = self
  17. }
  18. required init?(coder: NSCoder) {
  19. fatalError("init(coder:) has not been implemented")
  20. }
  21. public let sizeConfiguration: TabbedView.SizeConfiguration
  22. public lazy var tabbedView: TabbedView = {
  23. let tabbedView = TabbedView(
  24. sizeConfiguration: sizeConfiguration
  25. )
  26. return tabbedView
  27. }()
  28. public let pagedView = PagedView()
  29. // MARK: - UI Setup
  30. private func setupUI() {
  31. self.translatesAutoresizingMaskIntoConstraints = false
  32. self.addSubview(tabbedView)
  33. self.addSubview(pagedView)
  34. NSLayoutConstraint.activate([
  35. tabbedView.leftAnchor
  36. .constraint(equalTo: self.leftAnchor),
  37. tabbedView.topAnchor
  38. .constraint(equalTo: self.topAnchor),
  39. tabbedView.rightAnchor
  40. .constraint(equalTo: self.rightAnchor),
  41. tabbedView.heightAnchor
  42. .constraint(equalToConstant: sizeConfiguration.height)
  43. ])
  44. NSLayoutConstraint.activate([
  45. pagedView.leftAnchor
  46. .constraint(equalTo: self.leftAnchor),
  47. pagedView.topAnchor
  48. .constraint(equalTo: self.tabbedView.bottomAnchor),
  49. pagedView.rightAnchor
  50. .constraint(equalTo: self.rightAnchor),
  51. pagedView.bottomAnchor
  52. .constraint(equalTo: self.bottomAnchor)
  53. ])
  54. }
  55. }
  56. extension ViewPager: TabbedViewDelegate {
  57. func didMoveToTab(at index: Int) {
  58. self.pagedView.moveToPage(at: index)
  59. }
  60. }
  61. extension ViewPager: PagedViewDelegate {
  62. func didMoveToPage(index: Int) {
  63. self.tabbedView.moveToTab(at: index)
  64. }
  65. }