• Stars
    star
    2,235
  • Rank 19,757 (Top 0.5 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple view for building card like interface inspired by Tinder and Potluck.

ZLSwipeableViewSwift

A simple view for building card like interface like Tinder and Potluck. ZLSwipeableViewSwift is based on ZLSwipeableView.

Preview

Custom Animation

direction

Custom Swipe

direction

Custom Direction

direction

Undo

direction

Rewind

direction

CocoaPods

You can install ZLSwipeableViewSwift through CocoaPods adding the following to your Podfile:

pod 'ZLSwipeableViewSwift'
use_frameworks!

Then import it using:

import ZLSwipeableViewSwift

Carthage

You can install ZLSwipeableViewSwift through Carthage by adding github "zhxnlai/ZLSwipeableViewSwift" to your Cartfile

Swift Package Manager

You can install ZLSwipeableViewSwift through Swift Package Manager by adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/zhxnlai/ZLSwipeableViewSwift", .branchItem("master"))
]

Usage

Check out the demo app for an example. It contains the following demos: Default, Custom Animation, Custom Swipe, Allowed Direction, History, Previous View, Should Swipe and Always Swipe.

Instantiation

ZLSwipeableView can be added to storyboard or instantiated programmatically:

var swipeableView = ZLSwipeableView(frame: CGRect(x: 0, y: 0, width: 300, height: 500))
view.addSubview(swipeableView)

Adding Views

ZLSwipeableView supports both adding views to the front and to the back.

public var numberOfActiveView = UInt(4)
public var nextView: (() -> UIView?)?
public var previousView: (() -> UIView?)?

Adding views to the back

nextView, a closure that returns an UIView, works with loadViews and numberOfActiveView. It acts as the data source. After defining it, ZLSwipeableView will call loadViews which will invoke nextView numberOfActiveView times and insert them in the back. loadViews will also be called each time a view is swiped.

public func loadViews()
// Usage:
swipeableView.numberOfActiveView = 3
swipeableView.nextView = {
  return UIView()
}
swipeableView.loadViews() // optional, automatically call after nextView is set

Adding views to the front

previousView works with rewind, which inserts a view in the front and positions it at the center with animation. Note that rewind calls previousView only when history is empty, otherwise it returns the most recently swiped view. Please try out the Previous View example for more information.

public func rewind()
// Usage:
swipeableView.previousView = {
  return UIView()
}
swipeableView.rewind()

Interface Builder

If you need to work with Interface Builder, the demo app includes examples of both creating views programmatically and loading views from Xib files that use Auto Layout.

Removing Views

Swiping programmatically

The user can swipe views in the allowed directions. This can also happen programmatically.

You can swipe the top view programmatically in one of the predefined directions or with point and direction in the view's coordinate.

public func swipeTopView(inDirection direction: Direction)
// Usage:
swipeableView.swipeTopView(inDirection: .Left)
swipeableView.swipeTopView(inDirection: .Up)
swipeableView.swipeTopView(inDirection: .Right)
swipeableView.swipeTopView(inDirection: .Down)

public func swipeTopView(fromPoint location: CGPoint, inDirection directionVector: CGVector)
// Usage:
swipeableView.swipeTopView(fromPoint: CGPoint(x: 100, y: 30), inDirection: CGVector(dx: 100, dy: -800))

Rewinding

ZLSwipeableView keeps a history of swiped views. They can be retrieved by calling rewind.

public var history = [UIView]()
public var numberOfHistoryItem = UInt(10)

public func rewind()

Removing all views

To discard all views and reload programmatically (discarded views cannot by rewinded):

swipeableView.discardViews()
swipeableView.loadViews()

Delegate

Here is a list of callbacks you can listen to:

swipeableView.didStart = {view, location in
    print("Did start swiping view at location: \(location)")
}
swipeableView.swiping = {view, location, translation in
    print("Swiping at view location: \(location) translation: \(translation)")
}
swipeableView.didEnd = {view, location in
    print("Did end swiping view at location: \(location)")
}
swipeableView.didSwipe = {view, direction, vector in
    print("Did swipe view in direction: \(direction), vector: \(vector)")
}
swipeableView.didCancel = {view in
    print("Did cancel swiping view")
}

Customization

Here is a list of customizable behaviors:

public var animateView = ZLSwipeableView.defaultAnimateViewHandler()
public var interpretDirection = ZLSwipeableView.defaultInterpretDirectionHandler()
public var shouldSwipeView = ZLSwipeableView.defaultShouldSwipeViewHandler()
public var angle = CGFloat(1.0)
public var minTranslationInPercent = CGFloat(0.25)
public var minVelocityInPointPerSecond = CGFloat(750)
public var allowedDirection = Direction.Horizontal

interpretDirection

You can change how the direction gets interpreted by overriding the interpretDirection property. Take a look at the Custom Swipe example for details.

animateView

You can create custom animation by overriding the animateView property. Take a look at the Custom Animation example for details.

Should Swipe

shouldSwipeView, minTranslationInPercent and minVelocityInPointPerSecond determines whether a view should be swiped or not. Please see the Should Swipe example for details.

allowedDirection

The allowedDirection property limits the directions in which the user is allowed to swipe. Please see the Custom Direction example for details.

swipeableView.allowedDirection = .Left | .Up
swipeableView.allowedDirection = .All

angle

You can change the rotation of the swipe views with the 'angle' property

Misc

public func topView() -> UIView?

public func activeViews() -> [UIView]

Requirements

  • iOS 7 or higher.

Credits

Big thanks to the contributors of ZLSwipeableView.

License

ZLSwipeableViewSwift is available under MIT license. See the LICENSE file for more info.

More Repositories

1

ZLSwipeableView

A simple view for building card like interface inspired by Tinder and Potluck.
Objective-C
2,828
star
2

ZLBalancedFlowLayout

A UICollectionViewFlowLayout subclass that scales items to take up space, optimized for large item set, inspired by NHBalancedFlowLayout.
Swift
309
star
3

ZLSinusWaveView

A Siri like voice visualization view using EZAudio. Modified from SISinusWaveView for iOS.
Objective-C
283
star
4

ZLMusicFlowWaveView

A ZLSinusWaveView subclass inspired by 乐流/MusicFlow
Objective-C
202
star
5

ZLPeoplePickerViewController

A multilingual replacement for ABPeoplePickerNavigationController (address book picker) that supports UILocalized​Indexed​Collation
Objective-C
189
star
6

ZLHistogramAudioPlot

A hardware-accelerated audio visualization view using EZAudio, inspired by AudioCopy.
Objective-C
185
star
7

UIColor-ChineseTraditionalColors

A swift extension that extends UIColor with a list of Chinese traditional colors
Swift
132
star
8

AsyncTask

An asynchronous programming library for Swift
Swift
130
star
9

react-webgl-globe-basic-example

WebGL Globe basic example in react
JavaScript
70
star
10

Async

Async, await control flow for Swift.
Swift
69
star
11

Algorithms

Answers to LeetCode questions in Swift and JavaScript
Swift
52
star
12

ReactiveUI

A lightweight replacement for target action with closures, modified from Scream.swift.
Swift
51
star
13

Emoticon

An Emoticon Keyboard
Swift
22
star
14

wwdc

WWDC 2015 Student Scholarship App
Swift
6
star
15

printf

A web based visualizer for printf format string
JavaScript
6
star
16

YelpAPI

Yelp API in Swift
Swift
5
star
17

TicketsTonight

UCLA CS130 Fall 2014 Class Project
Objective-C
4
star
18

AlamofireAsync

Async extension for Alamofire
Swift
3
star
19

DongerListKeyboard

Experimenting with iOS 8 keyboard in swift
Objective-C
3
star
20

JSTester

A simple structure tester for javascript programs.
JavaScript
1
star
21

librarymanagement

Python
1
star
22

frappe_app_template

1
star