Skip to content

Commit

Permalink
add indexesForVisibleCards, unfocussed card tapping (#21)
Browse files Browse the repository at this point in the history
* added ability for unfocused cards to be tapped
* added indexesForVisibleCards to VerticalCardSwiperView (returns only cards in foreground)
  • Loading branch information
darnfish authored and JoniVR committed Nov 15, 2018
1 parent b425575 commit 04db8a6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Sources/CardCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import UIKit

self.layer.zPosition = CGFloat(layoutAttributes.zIndex)
}

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

Expand Down
10 changes: 5 additions & 5 deletions Sources/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ internal extension UIPanGestureRecognizer {
let velocity = self.velocity(in: view)
let vertical = abs(velocity.y) > abs(velocity.x)
switch (vertical, velocity.x, velocity.y) {
case (true, _, let y) where y < 0: return .Up
case (true, _, let y) where y > 0: return .Down
case (false, let x, _) where x > 0: return .Right
case (false, let x, _) where x < 0: return .Left
default: return PanDirection.None
case (true, _, let y) where y < 0: return .Up
case (true, _, let y) where y > 0: return .Down
case (false, let x, _) where x > 0: return .Right
case (false, let x, _) where x < 0: return .Left
default: return .None
}
}
}
18 changes: 4 additions & 14 deletions Sources/VerticalCardSwiper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,11 @@ extension VerticalCardSwiper: UIGestureRecognizerDelegate {
@objc fileprivate func handleTap(sender: UITapGestureRecognizer) {
if let delegate = delegate {
if let wasTapped = delegate.didTapCard {
/// The taplocation relative to the superview.
let location = sender.location(in: self)
/// The taplocation relative to the collectionView.
let locationInCollectionView = sender.location(in: verticalCardSwiperView)

if swipeAbleArea != nil {
if swipeAbleArea.contains(location) && !verticalCardSwiperView.isScrolling {
if let tappedCardIndex = verticalCardSwiperView.indexPathForItem(at: locationInCollectionView) {
wasTapped(verticalCardSwiperView, tappedCardIndex.row)
}
}
if let tappedCardIndex = verticalCardSwiperView.indexPathForItem(at: locationInCollectionView) {
wasTapped(verticalCardSwiperView, tappedCardIndex.row)
}
}
}
Expand All @@ -233,15 +227,11 @@ extension VerticalCardSwiper: UIGestureRecognizerDelegate {
@objc fileprivate func handleHold(sender: UILongPressGestureRecognizer) {
if let delegate = delegate {
if let wasHeld = delegate.didHoldCard {
/// The taplocation relative to the superview.
let location = sender.location(in: self)
/// The taplocation relative to the collectionView.
let locationInCollectionView = sender.location(in: verticalCardSwiperView)

if swipeAbleArea.contains(location) && !verticalCardSwiperView.isScrolling {
if let swipedCardIndex = verticalCardSwiperView.indexPathForItem(at: locationInCollectionView) {
wasHeld(verticalCardSwiperView, swipedCardIndex.row, sender.state)
}
if let swipedCardIndex = verticalCardSwiperView.indexPathForItem(at: locationInCollectionView) {
wasHeld(verticalCardSwiperView, swipedCardIndex.row, sender.state)
}
}
}
Expand Down
27 changes: 26 additions & 1 deletion Sources/VerticalCardSwiperView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ public class VerticalCardSwiperView: UICollectionView {
return (self.isDragging || self.isTracking || self.isDecelerating)
}

/**
Returns an array of indexes (as Int) that are currently visible in the `VerticalCardSwiperView`.
This does not include cards that are behind the card that is in focus.
- returns: An array of indexes (as Int) that are currently visible.
*/
public var indexesForVisibleCards: [Int] {

let lowestIndex = self.indexPathsForVisibleItems.min()?.row ?? 0

// when first card is focussed, return as usual.
if (visibleCells.count == 2 && lowestIndex == 0) {
return self.indexPathsForVisibleItems.map({$0.row}).sorted()
}

var indexes: [Int] = []
// Add each visible cell except the lowest one and return

for cellIndexPath in self.indexPathsForVisibleItems {
if (cellIndexPath.row != lowestIndex) {
indexes.append(cellIndexPath.row)
}
}
return indexes.sorted()
}

/**
Returns a reusable cell object located by its identifier.
Call this method from your data source object when asked to provide a new cell for the VerticalCardSwiperView.
Expand All @@ -48,7 +73,7 @@ public class VerticalCardSwiperView: UICollectionView {
- parameter identifier: The reuse identifier for the specified cell. This parameter must not be nil.

- parameter index: The index specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index to perform additional configuration based on the cell’s position in the VerticalCardSwiperView.
*/
*/
public func dequeueReusableCell(withReuseIdentifier identifier: String, for index: Int) -> UICollectionViewCell {
return self.dequeueReusableCell(withReuseIdentifier: identifier, for: IndexPath(row: index, section: 0))
}
Expand Down

0 comments on commit 04db8a6

Please sign in to comment.