diff --git a/Example/InfiniteLayout/Base.lproj/Main.storyboard b/Example/InfiniteLayout/Base.lproj/Main.storyboard index 2276247..a059869 100644 --- a/Example/InfiniteLayout/Base.lproj/Main.storyboard +++ b/Example/InfiniteLayout/Base.lproj/Main.storyboard @@ -203,6 +203,9 @@ + + + @@ -421,9 +424,6 @@ - - - diff --git a/Example/InfiniteLayout/BaseCollectionViewController.swift b/Example/InfiniteLayout/BaseCollectionViewController.swift index bacbf4a..2e650de 100644 --- a/Example/InfiniteLayout/BaseCollectionViewController.swift +++ b/Example/InfiniteLayout/BaseCollectionViewController.swift @@ -22,4 +22,8 @@ class BaseCollectionViewController: InfiniteCollectionViewController { override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 } + + override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + collectionView.scrollToItem(at: indexPath, at: self.infiniteCollectionView!.infiniteLayout.scrollDirection == .vertical ? .centeredVertically : .centeredHorizontally, animated: true) + } } diff --git a/Example/InfiniteLayout/CustomViewController.swift b/Example/InfiniteLayout/CustomViewController.swift index 411fbda..bf6fe12 100644 --- a/Example/InfiniteLayout/CustomViewController.swift +++ b/Example/InfiniteLayout/CustomViewController.swift @@ -31,6 +31,13 @@ extension CustomViewController: UICollectionViewDataSource { } } +extension CustomViewController: UICollectionViewDelegate { + + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true) + } +} + extension CustomViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { diff --git a/Example/InfiniteLayout/PickerController.swift b/Example/InfiniteLayout/PickerController.swift index c8af50e..542316a 100644 --- a/Example/InfiniteLayout/PickerController.swift +++ b/Example/InfiniteLayout/PickerController.swift @@ -30,6 +30,13 @@ extension PickerController: UICollectionViewDataSource { } } +extension PickerController: UICollectionViewDelegate { + + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) + } +} + extension PickerController: InfiniteCollectionViewDelegate { func infiniteCollectionView(_ infiniteCollectionView: InfiniteCollectionView, didChangeCenteredIndexPath centeredIndexPath: IndexPath?) { diff --git a/Example/InfiniteLayout/RxBaseCollectionViewController.swift b/Example/InfiniteLayout/RxBaseCollectionViewController.swift index 6bac698..8c230bd 100644 --- a/Example/InfiniteLayout/RxBaseCollectionViewController.swift +++ b/Example/InfiniteLayout/RxBaseCollectionViewController.swift @@ -35,5 +35,11 @@ class RxBaseCollectionViewController: UIViewController { } print("centered: \(current + 1)") }).disposed(by: disposeBag) + + infiniteCollectionView.rx.itemSelected + .asDriver() + .drive(onNext: { [unowned self] indexPath in + self.infiniteCollectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true) + }).disposed(by: disposeBag) } } diff --git a/InfiniteLayout/Classes/InfiniteLayout.swift b/InfiniteLayout/Classes/InfiniteLayout.swift index 31729b6..8701e2d 100644 --- a/InfiniteLayout/Classes/InfiniteLayout.swift +++ b/InfiniteLayout/Classes/InfiniteLayout.swift @@ -143,6 +143,7 @@ open class InfiniteLayout: UICollectionViewFlowLayout { let offset = self.preferredContentOffset(forContentOffset: collectionView.contentOffset) if (page < 2 || page > self.multiplier - 2) && collectionView.contentOffset != offset { collectionView.contentOffset = offset + DispatchQueue.main.async(execute: self.invalidateLayout) } } @@ -249,6 +250,7 @@ open class InfiniteLayout: UICollectionViewFlowLayout { return } collectionView.contentOffset = offset + DispatchQueue.main.async(execute: self.invalidateLayout) } // MARK: Copy