Skip to content

Instantly share code, notes, and snippets.

@gkye
Last active June 7, 2016 21:24
Show Gist options
  • Save gkye/329d61b9cf660c6fb7b203587f39a4c6 to your computer and use it in GitHub Desktop.
Save gkye/329d61b9cf660c6fb7b203587f39a4c6 to your computer and use it in GitHub Desktop.

Swiper

UIView sublass for creating Tinder like swipe cards, with a peek view.

Usage

Check out the demo app for a detailed example.

Adding Swiper View

A Swiper view can be added via storyboard or programmatically

var swiperView = Swiper(frame: CGRect(x: 0, y: 0, width: 350, height: 350)))
view.addSubview(swiperView)
swipeView.delegate = self
swipeView.dataSource = self

Protocols

DataSource

Returns the images to be displayed within the Swiper view.

public func cardData() -> [SwiperData]

Returns the number of items to be displayed by the Swiper view.

public func numberOfCards()->Int

Delegate

Method is called when the Swiper view is first display and each time a swipe actions occurs. Returns the current index of the card and its dataSource

public func didShowCardAtIndex(index: Int, dataSource: SwiperData)

Method is called when the Swiper view is swipped left or programtically undoSwipe. Returns the current index of the card and its dataSource

public func didUndoAction(index: Int, dataSource: SwiperData)

Method is called when the Swiper view is tapped. Returns the current index of the card of the current item

public func didSelectCardAtIndex(index: Int)

Method is called when the Swiper view is no more items to be displayed from its current dataSource

public func cardsDidRunOut(status: Bool)

Controlling SwiperView

Swiper supports both right and left swipe actions. Swiper Right to move to the next card and Swipe left to undo an action. These actions are also supported programtically.

public func swipeToNext()
public func undoSwipe()

Reloads the Swiper views items from its current dataSource and refreshes display.

public func reloadData()

Using the PeekView

Must the peekViewEnabed = true and conform to the peekViewDelegate in order to use the PeekView

    swipeView.peekViewEnabled = true
    swipeView.peekViewDelegate = self

PeekView Attributes

In order for the peekView to function a parentViewController and contentViewController must be set (actions are optional). Below is an example.

func swiperPeekViewAttributes() -> SwiperPeekViewAttributes {
    let controller = storyboard?.instantiateViewControllerWithIdentifier("PeekViewController") as! PeekViewController
    controller.image = imgs[currentIndex].image
    controller.labelText = imgs[currentIndex].title
    
    let actions = [
      PeekViewAction(title: "Like", style: .Selected),
      PeekViewAction(title: "Show Details", style: .Default),
      PeekViewAction(title: "Cancel", style: .Destructive) ]
    let attributes = SwiperPeekViewAttributes.init(parentVC: self, contentVC: controller, actions: actions)
    return attributes
  }

Handling PeekView actions

Returns the selected index of selected action from the PeekView

func swiperPeekViewDidSelectAtionAtIndex(index: Int)

Dependencies

TODO

  • Create CocoaPods
  • Fix dependency issues with Stellar
  • Remove Open Source folder and add PeekView and Stellar as dependency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment