76

GitHub - JoniVR/VerticalCardSwiper: A marriage between the Shazam Discover UI an...

 6 years ago
source link: https://github.com/JoniVR/VerticalCardSwiper
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

VerticalCardSwiper

A marriage between the Shazam Discover UI and Tinder, built with UICollectionView in Swift.

example

Build Status Version License Platform

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

  • iOS 9.0
  • Swift 4.1

Installation

VerticalCardSwiper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'VerticalCardSwiper', '0.1.0-beta2'

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 = view as? VerticalCardSwiper
        cardSwiper.datasource = self
        
        // register cardcell for storyboard use
        cardSwiper.register(nib: UINib(nibName: "ExampleCell", bundle: nil), forCellWithReuseIdentifier: "ExampleCell")
    }
    
    func cardForItemAt(verticalCardSwiperView: VerticalCardSwiperView, cardForItemAt index: Int) -> CardCell {
        
        let cardCell = verticalCardSwiperView.dequeueReusableCell(withReuseIdentifier: "ExampleCell", for: index) as! ExampleCardCell
                
        return cardCell
    }
    
    func numberOfCards(verticalCardSwiperView: VerticalCardSwiperView) -> Int {
        return 100
    }
}

Properties

/// Indicates if side swiping on cards is enabled. Set to false if you don't want side swiping. Default value is `true`.
@IBInspectable public var isSideSwipingEnabled: Bool = true
/// The inset (spacing) at the top for the cards. Default is 40.
@IBInspectable public var topInset: CGFloat = 40
/// The inset (spacing) at each side of the cards. Default is 20.
@IBInspectable public var sideInset: CGFloat = 20
/// Sets how much of the next card should be visible. Default is 50.
@IBInspectable public var visibleNextCardHeight: CGFloat = 50
/// Vertical spacing between CardCells. Default is 40.
@IBInspectable public var cardSpacing: CGFloat = 40
/// The transform animation that is shown on the top card when scrolling through the cards. Default is 0.05.
@IBInspectable public var firstItemTransform: CGFloat = 0.05

Other

Just like with a regular UICollectionView, you can reload the data by calling:

cardSwiper.reloadData()

Delegation

To handle swipe gestures, implement the VerticalCardSwiperDelegate.

class ViewController: UIViewController, VerticalCardSwiperDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        cardSwiper.delegate = self
    }

    func didSwipeCardAway(card: CardCell, index: Int, swipeDirection: CellSwipeDirection) {

        // handle swipe gestures
    }
    
    func willSwipeCardAway(card: CardCell, index: Int, swipeDirection: CellSwipeDirection) {
    
        // called right before the card animates off the screen (optional).
    }
    
    func sizeForItem(verticalCardSwiperView: VerticalCardSwiperView, index: Int) -> CGSize {
    
        // Allows you to return custom card sizes (optional).
        return CGSize(width: verticalCardSwiperView.frame.width * 0.75, height: verticalCardSwiperView.frame.height * 0.75)
    }
}

Customization

Subclass the CardCell to customize the cards.

class ExampleCardCell: CardCell {

} 

Features

  • Shazam Discover UI with paging
  • Tinder-style swiping
  • Option to disable side swiping
  • Code documentation in README.md file
  • Cocoapods support
  • Carthage support
  • Diff support

Author

Joni Van Roost, [email protected]

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK