Skip to content

Commit

Permalink
thread fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddorgans committed Jan 4, 2018
1 parent e95b550 commit 9468de3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Example/InfiniteLayout/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@
</cells>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="isItemPagingEnabled" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="velocityMultiplier">
<real key="value" value="500"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<outlet property="dataSource" destination="ctO-Ld-yhL" id="bXu-Jz-K7t"/>
Expand Down Expand Up @@ -421,9 +424,6 @@
</cells>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="isItemPagingEnabled" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="velocityMultiplier">
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<outlet property="dataSource" destination="CU7-Hg-6kG" id="BOZ-dL-WLG"/>
Expand Down
4 changes: 4 additions & 0 deletions Example/InfiniteLayout/BaseCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions Example/InfiniteLayout/CustomViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions Example/InfiniteLayout/PickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
6 changes: 6 additions & 0 deletions Example/InfiniteLayout/RxBaseCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions InfiniteLayout/Classes/InfiniteLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -249,6 +250,7 @@ open class InfiniteLayout: UICollectionViewFlowLayout {
return
}
collectionView.contentOffset = offset
DispatchQueue.main.async(execute: self.invalidateLayout)
}

// MARK: Copy
Expand Down

0 comments on commit 9468de3

Please sign in to comment.