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

Support scroll multiple items #19

Open
wants to merge 2 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
4 changes: 2 additions & 2 deletions UPCarouselFlowLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = "UPCarouselFlowLayout"
s.version = "1.1.1"
s.version = "1.1.2"
s.summary = "A fancy carousel flow layout for UICollectionView."
s.description = "UPCarouselFlowLayout is a fancy carousel flow layout for UICollectionView. It comes with a paginated effect and it shrinks and makes transparent the side items."

s.homepage = "https://github.com/ink-spot/UPCarouselFlowLayout"
# s.screenshots = "https://github.com/ink-spot/UPCarouselFlowLayout/raw/master/images/demo.gif"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Paul Ulric' => '[email protected]' }
s.source = { :git => 'https://github.com/ink-spot/UPCarouselFlowLayout.git', :tag => s.version.to_s }
s.source = { :git => 'https://github.com/dangthaison91/UPCarouselFlowLayout', :tag => s.version.to_s }

s.ios.deployment_target = '8.1'

Expand Down
25 changes: 18 additions & 7 deletions UPCarouselFlowLayout/UPCarouselFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ open class UPCarouselFlowLayout: UICollectionViewFlowLayout {
@IBInspectable open var sideItemScale: CGFloat = 0.6
@IBInspectable open var sideItemAlpha: CGFloat = 0.6
@IBInspectable open var sideItemShift: CGFloat = 0.0
open var decelerationRate: CGFloat = UIScrollViewDecelerationRateFast
open var canScrollMultiItems: Bool = false
open var spacingMode = UPCarouselFlowLayoutSpacingMode.fixed(spacing: 40)

fileprivate var state = LayoutState(size: CGSize.zero, direction: .horizontal)
Expand All @@ -47,8 +49,8 @@ open class UPCarouselFlowLayout: UICollectionViewFlowLayout {

fileprivate func setupCollectionView() {
guard let collectionView = self.collectionView else { return }
if collectionView.decelerationRate != UIScrollViewDecelerationRateFast {
collectionView.decelerationRate = UIScrollViewDecelerationRateFast
if collectionView.decelerationRate != decelerationRate {
collectionView.decelerationRate = decelerationRate
}
}

Expand Down Expand Up @@ -114,12 +116,21 @@ open class UPCarouselFlowLayout: UICollectionViewFlowLayout {
}

override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView , !collectionView.isPagingEnabled,
let layoutAttributes = self.layoutAttributesForElements(in: collectionView.bounds)
else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset) }
guard let collectionView = collectionView, !collectionView.isPagingEnabled else {
return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
}

let isHorizontal = (self.scrollDirection == .horizontal)

var targetRect = collectionView.bounds

if canScrollMultiItems {
targetRect.origin = isHorizontal ? CGPoint(x: proposedContentOffset.x, y: 0.0) : CGPoint(x: 0.0, y: proposedContentOffset.y)
}

guard let layoutAttributes = self.layoutAttributesForElements(in: targetRect) else {
return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
}

let midSide = (isHorizontal ? collectionView.bounds.size.width : collectionView.bounds.size.height) / 2
let proposedContentOffsetCenterOrigin = (isHorizontal ? proposedContentOffset.x : proposedContentOffset.y) + midSide

Expand Down