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 294fd76 commit a522cf0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Example/InfiniteLayout/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<objects>
<navigationController id="yGQ-hH-2v6" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" largeTitles="YES" id="IdE-R7-9TG">
<rect key="frame" x="0.0" y="20" width="320" height="93"/>
<rect key="frame" x="0.0" y="20" width="320" height="96"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
Expand Down Expand Up @@ -403,7 +403,7 @@
</collectionViewFlowLayout>
<cells>
<collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" reuseIdentifier="cell" id="BVQ-cb-bj4" customClass="Cell" customModule="InfiniteLayout_Example" customModuleProvider="target">
<rect key="frame" x="0.0" y="152.5" width="250" height="150"/>
<rect key="frame" x="0.0" y="151" width="250" height="150"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO">
<rect key="frame" x="0.0" y="0.0" width="250" height="150"/>
Expand Down
2 changes: 1 addition & 1 deletion InfiniteLayout.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'InfiniteLayout'
s.version = '0.2.0.3'
s.version = '0.2.1'
s.summary = 'Horizontal and Vertical infinite scrolling feature for UICollectionView with Paging, NSProxy delegate, Reactive extension'

# This description is used to generate tags and improve search results.
Expand Down
23 changes: 13 additions & 10 deletions InfiniteLayout/Classes/InfiniteCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ open class InfiniteCollectionView: UICollectionView {

open override func layoutSubviews() {
super.layoutSubviews()

self.loopCollectionViewIfNeeded()
self.updateLayoutIfNeeded()
}
}

Expand Down Expand Up @@ -165,15 +164,9 @@ extension InfiniteCollectionView: UICollectionViewDataSource {

extension InfiniteCollectionView: UICollectionViewDelegate {

// MARK: Loop
func loopCollectionViewIfNeeded() {
self.infiniteLayout.loopCollectionViewIfNeeded()
self.centerCollectionViewIfNeeded()
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
delegateProxy.delegate?.scrollViewDidScroll?(scrollView)
func updateLayoutIfNeeded() {
self.loopCollectionViewIfNeeded()
self.centerCollectionViewIfNeeded()

let preferredVisibleIndexPath = infiniteLayout.preferredVisibleLayoutAttributes()?.indexPath
if self.centeredIndexPath != preferredVisibleIndexPath {
Expand All @@ -182,6 +175,16 @@ extension InfiniteCollectionView: UICollectionViewDelegate {
}
}

// MARK: Loop
func loopCollectionViewIfNeeded() {
self.infiniteLayout.loopCollectionViewIfNeeded()
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
delegateProxy.delegate?.scrollViewDidScroll?(scrollView)
self.updateLayoutIfNeeded()
}

// MARK: Paging
func centerCollectionViewIfNeeded() {
guard isItemPagingEnabled,
Expand Down
17 changes: 11 additions & 6 deletions InfiniteLayout/Classes/InfiniteLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open class InfiniteLayout: UICollectionViewFlowLayout {

public var velocityMultiplier: CGFloat = 1 // used to simulate paging

private let multiplier: CGFloat = 100 // contentOffset multiplier
private let multiplier: CGFloat = 500 // contentOffset multiplier

private var contentSize: CGSize = .zero

Expand Down Expand Up @@ -131,6 +131,13 @@ open class InfiniteLayout: UICollectionViewFlowLayout {
}

// MARK: Loop
private func updateContentOffset(_ offset: CGPoint) {
guard let collectionView = self.collectionView else {
return
}
collectionView.contentOffset = offset
collectionView.layoutIfNeeded()
}
private func preferredContentOffset(forContentOffset contentOffset: CGPoint) -> CGPoint {
return rect(from: CGRect(origin: contentOffset, size: .zero), page: self.page(from: .zero, offset: multiplier / 2)).origin
}
Expand All @@ -142,8 +149,7 @@ open class InfiniteLayout: UICollectionViewFlowLayout {
let page = self.pageIndex(from: self.page(for: collectionView.contentOffset))
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)
self.updateContentOffset(offset)
}
}

Expand Down Expand Up @@ -245,12 +251,11 @@ open class InfiniteLayout: UICollectionViewFlowLayout {
return
}
guard let preferredAttributes = self.preferredVisibleLayoutAttributes(indexPath: indexPath),
let offset = self.centeredContentOffset(forRect: preferredAttributes.frame),
let offset = self.centeredContentOffset(forRect: preferredAttributes.frame),
collectionView.contentOffset != offset else {
return
}
collectionView.contentOffset = offset
DispatchQueue.main.async(execute: self.invalidateLayout)
self.updateContentOffset(offset)
}

// MARK: Copy
Expand Down

0 comments on commit a522cf0

Please sign in to comment.