VerticalCardSwiper
A marriage between the Shazam Discover UI and Tinder, built with UICollectionView in Swift.
Project goal and information
The goal of this project is to recreate the Discover UI in Shazam (which I think is a great, fun way to display content) in combination with a Tinder style of swiping cards to the left/right.
The idea behind this is that in some cases, you don't want to swipe away cards, but keep them available for later on. This implementation allows for that. And it's a fun way to interact with content.
It's built with a UICollectionView
and a custom flowLayout.
Requirements
Installation
CocoaPods
To install with CocoaPods, simply add the following line to your Podfile:
pod 'VerticalCardSwiper'
Carthage
To install with Carthage, simply add the following line to your Podfile:
github "JoniVR/VerticalCardSwiper"
Example
To try out VerticalCardSwiper
pod try VerticalCardSwiper
or open the project and run the Example.
Usage
VerticalCardSwiper
behaves a lot like a standard UICollectionView
.
To use it inside your UIViewController
:
import VerticalCardSwiper
class ExampleViewController: UIViewController, VerticalCardSwiperDatasource {
private var cardSwiper: VerticalCardSwiper!
override func viewDidLoad() {
super.viewDidLoad()
cardSwiper = VerticalCardSwiper(frame: self.view.bounds)
view.addSubview(cardSwiper)
cardSwiper.datasource = self
cardSwiper.register(nib: UINib(nibName: "ExampleCell", bundle: nil), forCellWithReuseIdentifier: "ExampleCell")
}
func cardForItemAt(verticalCardSwiperView: VerticalCardSwiperView, cardForItemAt index: Int) -> CardCell {
if let cardCell = verticalCardSwiperView.dequeueReusableCell(withReuseIdentifier: "ExampleCell", for: index) as? ExampleCardCell {
return cardCell
}
return CardCell()
}
func numberOfCards(verticalCardSwiperView: VerticalCardSwiperView) -> Int {
return 100
}
}
Properties
@IBInspectable public var isSideSwipingEnabled: Bool = true
@IBInspectable public var isStackingEnabled: Bool = true
@IBInspectable public var firstItemTransform: CGFloat = 0.05
@IBInspectable public var topInset: CGFloat = 40
@IBInspectable public var sideInset: CGFloat = 20
@IBInspectable public var visibleNextCardHeight: CGFloat = 50
@IBInspectable public var cardSpacing: CGFloat = 40
@IBInspectable public var isStackOnBottom: Bool = true
@IBInspectable public var stackedCardsCount: Int = 1
public var indexesForVisibleCards: [Int]
Other
Just like with a regular UICollectionView
, you can reload the data by calling
cardSwiper.reloadData()
Get the current focussed card index
cardSwiper.focussedCardIndex
Scroll to a specifc card by calling
cardSwiper.scrollToCard(at: Int, animated: Bool) -> Bool
Get a card at a specified index
cardSwiper.cardForItem(at: Int) -> CardCell?
Swipe a card away programatically
cardSwiper.swipeCardAwayProgrammatically(at: Int, to: SwipeDirection, withDuration: TimeInterval = 0.3) -> Bool
Moving/Deleting/Inserting cards at runtime
Make sure to update your datasource first, otherwise an error will occur.
cardSwiper.moveCard(at: Int, to: Int)
cardSwiper.deleteCards(at: [Int])
cardSwiper.insertCards(at: [Int])
Delegation
To handle swipe gestures, implement the VerticalCardSwiperDelegate
.
class ViewController: UIViewController, VerticalCardSwiperDelegate {
override func viewDidLoad() {
super.viewDidLoad()
cardSwiper.delegate = self
}
func willSwipeCardAway(card: CardCell, index: Int, swipeDirection: SwipeDirection) {
}
func didSwipeCardAway(card: CardCell, index: Int, swipeDirection: SwipeDirection) {
}
func didCancelSwipe(card: CardCell, index: Int) {
}
func sizeForItem(verticalCardSwiperView: VerticalCardSwiperView, index: Int) -> CGSize {
return CGSize(width: verticalCardSwiperView.frame.width * 0.75, height: verticalCardSwiperView.frame.height * 0.75)
}
func didScroll(verticalCardSwiperView: VerticalCardSwiperView) {
}
func didEndScroll(verticalCardSwiperView: VerticalCardSwiperView) {
}
func didDragCard(card: CardCell, index: Int, swipeDirection: SwipeDirection) {
}
func didTapCard(verticalCardSwiperView: VerticalCardSwiperView, index: Int) {
}
func didHoldCard(verticalCardSwiperView: VerticalCardSwiperView, index: Int, state: UIGestureRecognizer.State) {
}
}
Customization
Subclass the CardCell
to customize the cards.
class ExampleCardCell: CardCell {
}
Key Features
Author
Joni Van Roost, joni.VR@hotmail.com
License
VerticalCardSwiper is available under the MIT license. See the LICENSE file for more info.
More
Feel free to submit a pull request, open an issue or fork this project. Any help is always appreciated.
A big thank you to all the contributors!