From 00474bf3bbe0faaa395622773dabe397b2d9c09d Mon Sep 17 00:00:00 2001 From: WenchaoD Date: Tue, 21 Feb 2017 22:33:16 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=98scrollOffset=E2=80=99=20property?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FSPagerView/FSPagerView.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/FSPagerView/FSPagerView.swift b/FSPagerView/FSPagerView.swift index de5cc98..e379a84 100644 --- a/FSPagerView/FSPagerView.swift +++ b/FSPagerView/FSPagerView.swift @@ -153,6 +153,11 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega return self.collectionView.isTracking } + open var scrollOffset: CGFloat { + let scrollOffset = Double(self.collectionView.contentOffset.x.divided(by: self.collectionViewLayout.itemSpan)) + return fmod(CGFloat(scrollOffset), CGFloat(Double(self.numberOfItems))) + } + // MARK: - Public readonly-properties open fileprivate(set) dynamic var currentIndex: Int = 0 @@ -312,7 +317,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega public func scrollViewDidScroll(_ scrollView: UIScrollView) { if self.numberOfItems > 0 { // In case someone is using KVO - let currentIndex = lround(Double(scrollView.contentOffset.x.divided(by: self.collectionViewLayout.itemSpan))) % self.numberOfItems + let currentIndex = lround(Double(self.scrollOffset)) if (currentIndex != self.currentIndex) { self.currentIndex = currentIndex }