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

//
// PagedView.swift
// GME Remit
//
// Created by Armaan Shrestha on 21/08/2022.
// Copyright © 2022 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
protocol PagedViewDelegate: AnyObject {
func didMoveToPage(index: Int)
}
class PagedView: UIView, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
// MARK: - Initialization
init(pages: [UIView] = []) {
self.pages = pages
super.init(frame: .zero)
self.setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Properties
public weak var delegate: PagedViewDelegate?
public var pages: [UIView] {
didSet {
self.collectionView.reloadData()
}
}
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(
frame: .zero,
collectionViewLayout: layout
)
collectionView.showsHorizontalScrollIndicator = false
collectionView.isPagingEnabled = true
collectionView.register(PageCollectionViewCell.self, forCellWithReuseIdentifier: "PageCollectionViewCell")
collectionView.delegate = self
collectionView.dataSource = self
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}()
// MARK: - UI Setup
func setupUI() {
self.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(collectionView)
collectionView.backgroundColor = .white
NSLayoutConstraint.activate([
collectionView.widthAnchor
.constraint(equalTo: self.widthAnchor),
collectionView.heightAnchor
.constraint(equalTo: self.heightAnchor),
collectionView.centerXAnchor
.constraint(equalTo: self.centerXAnchor),
collectionView.centerYAnchor
.constraint(equalTo: self.centerYAnchor)
])
}
// MARK: - Data Source
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PageCollectionViewCell", for: indexPath) as! PageCollectionViewCell
let page = self.pages[indexPath.item]
cell.view = page
return cell
}
// MARK: - Actions
public func moveToPage(at index: Int) {
self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
}
// MARK: - Delegate
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let page = Int(self.collectionView.contentOffset.x / self.collectionView.frame.size.width)
self.delegate?.didMoveToPage(index: page)
}
// MARK: - Layout Delegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView.frame.width,
height: self.collectionView.frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}