Created
December 19, 2018 14:27
-
-
Save alouanemed/831ec9e05929d669344e5f4000655d86 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 | |
import RxSwift | |
import RxCocoa | |
import RxDataSources | |
private let reuseIdentifier = "ProductImageItemCell" | |
protocol ProductHeaderCarouselDelegate { | |
func didTapItem(indexPath: IndexPath, selectedImage: ImageView) | |
} | |
class ProductHeaderCarouselController: UICollectionViewController { | |
var viewModel: ProductHeaderViewModel! | |
var delegate: ProductHeaderCarouselDelegate? | |
lazy var pageControl: UIPageControl = { | |
let pc = UIPageControl() | |
pc.currentPage = 0 | |
pc.currentPageIndicatorTintColor = UIColor.primaryDark() | |
pc.pageIndicatorTintColor = UIColor.secondary() | |
return pc | |
}() | |
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { | |
let x = targetContentOffset.pointee.x | |
pageControl.currentPage = Int(x / view.frame.width) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
collectionView.register(cellWithClass: ProductImageItemCell.self) | |
setupBottomControls() | |
collectionView?.backgroundColor = .white | |
collectionView?.register(ProductImageItemCell.self, forCellWithReuseIdentifier: "cellId") | |
if let layout = collectionViewLayout as? UICollectionViewFlowLayout { | |
layout.scrollDirection = .horizontal | |
layout.minimumLineSpacing = 0 | |
layout.minimumInteritemSpacing = 0 | |
} | |
collectionView?.isPagingEnabled = true | |
} | |
func setupBottomControls() { | |
view.addSubview(pageControl) | |
pageControl.snp.makeConstraints { (make) -> Void in | |
make.leading.equalTo(view.safeAreaLayoutGuide.snp.leading) | |
make.trailing.equalTo(view.safeAreaLayoutGuide.snp.trailing) | |
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom) | |
make.height.equalTo(50) | |
} | |
} | |
func bindViewModel() { | |
viewModel.transform().asDriver(onErrorJustReturn: []) | |
.drive(self.collectionView.rx.items(cellIdentifier: reuseIdentifier, cellType: ProductImageItemCell.self)) { collectionView, viewModel, cell in | |
cell.bind(to: viewModel) | |
}.disposed(by: rx.disposeBag) | |
viewModel.map{ | |
pageControl.numberOfPages = $0.elements.value.count | |
} | |
} | |
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
let selectedCell = collectionView.cellForItem(at: indexPath) as! ProductImageItemCell | |
delegate?.didTapItem(indexPath: indexPath, selectedImage: selectedCell.productImageView) | |
} | |
} | |
extension ProductHeaderCarouselController { | |
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { | |
coordinator.animate(alongsideTransition: { (_) in | |
self.collectionViewLayout.invalidateLayout() | |
if self.pageControl.currentPage == 0 { | |
self.collectionView?.contentOffset = .zero | |
} else { | |
let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0) | |
self.collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) | |
} | |
}) { (_) in | |
} | |
} | |
} | |
extension ProductHeaderCarouselController: UICollectionViewDelegateFlowLayout { | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CGSize(width: view.frame.width, height: view.frame.height) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment