Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pager view visibleCells property #106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
24 changes: 24 additions & 0 deletions Sources/FSPagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
return self.collectionView.panGestureRecognizer
}

/// Returns an array of visible cells currently displayed by the pager view.
open var visibleCells: [FSPagerViewCell] {
return self.collectionView.visibleCells as! [FSPagerViewCell]
}

/// To disable or enable multi scrolling interaction
open var isMultiScrollInteractionEnable = true

@objc open fileprivate(set) dynamic var currentIndex: Int = 0

// MARK: - Private properties
Expand Down Expand Up @@ -260,6 +268,9 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
return animationKeys.contains(where: { rotationAnimationKeys.contains($0) })
}
fileprivate var possibleTargetingIndexPath: IndexPath?
fileprivate var offsetAtBegin:CGPoint? {
didSet { if isMultiScrollInteractionEnable { offsetAtBegin = nil } }
}

// MARK: - Overriden functions

Expand Down Expand Up @@ -391,6 +402,14 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let offset = offsetAtBegin {
let pageSide:CGFloat = collectionViewLayout.itemSpacing
if scrollView.contentOffset.x > offset.x + pageSide {
scrollView.contentOffset.x = offset.x + pageSide
} else if scrollView.contentOffset.x < offset.x - pageSide {
scrollView.contentOffset.x = offset.x - pageSide
}
}
if !self.isPossiblyRotating && self.numberOfItems > 0 {
// In case someone is using KVO
let currentIndex = lround(Double(self.scrollOffset)) % self.numberOfItems
Expand All @@ -411,6 +430,7 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
if self.automaticSlidingInterval > 0 {
self.cancelTimer()
}
offsetAtBegin = scrollView.contentOffset
}

public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
Expand All @@ -422,12 +442,16 @@ open class FSPagerView: UIView,UICollectionViewDataSource,UICollectionViewDelega
if self.automaticSlidingInterval > 0 {
self.startTimer()
}
if velocity == .zero {
offsetAtBegin = nil
}
}

public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if let function = self.delegate?.pagerViewDidEndDecelerating {
function(self)
}
offsetAtBegin = nil
}

public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
Expand Down