From a522cf0c36bbb310631ddfea2d0b005866cae721 Mon Sep 17 00:00:00 2001 From: Arnaud Dorgans Date: Thu, 4 Jan 2018 17:42:22 +0100 Subject: [PATCH] thread fix --- .../InfiniteLayout/Base.lproj/Main.storyboard | 4 ++-- InfiniteLayout.podspec | 2 +- .../Classes/InfiniteCollectionView.swift | 23 +++++++++++-------- InfiniteLayout/Classes/InfiniteLayout.swift | 17 +++++++++----- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Example/InfiniteLayout/Base.lproj/Main.storyboard b/Example/InfiniteLayout/Base.lproj/Main.storyboard index 9d87737..d67c94c 100644 --- a/Example/InfiniteLayout/Base.lproj/Main.storyboard +++ b/Example/InfiniteLayout/Base.lproj/Main.storyboard @@ -16,7 +16,7 @@ - + @@ -403,7 +403,7 @@ - + diff --git a/InfiniteLayout.podspec b/InfiniteLayout.podspec index 013b101..5d50f7c 100644 --- a/InfiniteLayout.podspec +++ b/InfiniteLayout.podspec @@ -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. diff --git a/InfiniteLayout/Classes/InfiniteCollectionView.swift b/InfiniteLayout/Classes/InfiniteCollectionView.swift index 6a8f245..ebe16ac 100644 --- a/InfiniteLayout/Classes/InfiniteCollectionView.swift +++ b/InfiniteLayout/Classes/InfiniteCollectionView.swift @@ -112,8 +112,7 @@ open class InfiniteCollectionView: UICollectionView { open override func layoutSubviews() { super.layoutSubviews() - - self.loopCollectionViewIfNeeded() + self.updateLayoutIfNeeded() } } @@ -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 { @@ -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, diff --git a/InfiniteLayout/Classes/InfiniteLayout.swift b/InfiniteLayout/Classes/InfiniteLayout.swift index 8701e2d..e52e2b4 100644 --- a/InfiniteLayout/Classes/InfiniteLayout.swift +++ b/InfiniteLayout/Classes/InfiniteLayout.swift @@ -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 @@ -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 } @@ -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) } } @@ -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