HorizontalCardsView 0.2.0

HorizontalCardsView 0.2.0

Maintained by Alex Smetannikov.



Cocoapods Platform Language License: MIT

HorizontalCardsView

Visual component realizes a horizontally scrollable list of cards.

Scrolling example

Installation

CocoaPods

CocoaPods is the preffered way to add HorizontalCardsView to your project.

Just add following line to your Podfile

pod 'HorizontalCardsView'

Then run a pod install inside your terminal.

After that you can include HorizontalCardsView wherever you need it with

import HorizontalCardsView

Usage

  1. Add UIView to storyboard and set Custom Class as HorizontalCardsView.

Storyboard

  1. Set you ViewController to conform HorizontalCardsDataSource and HorizontalCardsDelegateprotocols.
  2. Setup params viewsSource and delegate in viewDidLoad() for your HorizontalCardsView instance. Also, you should define params cardSpasing and insets. Height of cards is calculated as the height of HorizontalCardsView minus top and bottom insets. WIdth of cards is calculated as the width of HorizontalCardsView with cardWidthFactor or width of HorizontalCardsView minus left and right insets if the component contains a single card.
class ViewController: UIViewController, HorizontalCardsDataSource, HorizontalCardsDelegate {

    @IBOutlet weak var horizontalCardsView: HorizontalCardsView!

    // ...
    
    override func viewDidLoad() {
        horizontalCardsView.dataSource = self
        horizontalCardsView.delegate = self
        horizontalCardsView.cellSpacing = 15
        horizontalCardsView.inset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    // ...
    
    func horizontalCardsViewNumberOfItems(_ collectionView: HorizontalCardsView) -> Int {
        return 5
    }

    func horizontalCardsView(_: HorizontalCardsView, viewForIndex index: Int) -> HorizontalCardView {
        let bundle = Bundle(for: SampleView.self)
        let view = bundle.loadNibNamed("SampleView", owner: self, options: nil)?.first

        return view
    }
    
    func horizontalCardsView(_: HorizontalCardsView, didSelectItemAtIndex index: Int) {
        print("A view with index \(index) was selected.")
    }
    
    // ...
    
   }
  1. Views which you want to display in the scrollable list must be descendants of HorizontalCardView class. Override prepareForReuse() method, if you want to do some actions before views will be reused.