ZLSwipeableViewSwift
一个简单的视图,用于构建类似于 Tinder 和 Potluck 的卡片式界面。ZLSwipeableViewSwift 基于 ZLSwipeableView。
预览
自定义动画
自定义滑动
自定义方向
撤销
回放
CocoaPods
您可以通过CocoaPods安装 ZLSwipeableViewSwift
,在您的Podfile中添加以下内容
pod 'ZLSwipeableViewSwift'
use_frameworks!
然后导入它
import ZLSwipeableViewSwift
Carthage
您也可以通过Carthage安装 ZLSwipeableViewSwift
,在您的Cartfile中添加以下内容 github "zhxnlai/ZLSwipeableViewSwift"
使用方法
查看示例应用程序获取示例。它包含以下示例:默认配置、自定义动画、自定义滑动、允许的滑动方向、历史记录、以前的视图、应滑动和始终滑动。
实例化
ZLSwipeableView
可以添加到Storyboard或以编程方式实例化
var swipeableView = ZLSwipeableView(frame: CGRect(x: 0, y: 0, width: 300, height: 500))
view.addSubview(swipeableView)
支持将视图添加到前端和后端。
ZLSwipeableView
支持将视图添加到前端和后端。
public var numberOfActiveView = UInt(4)
public var nextView: (() -> UIView?)?
public var previousView: (() -> UIView?)?
将视图添加到后端
nextView
,一个返回 UIView
的闭包,配合 loadViews
和 numberOfActiveView
使用。它充当数据源。定义后,ZLSwipeableView
将调用 loadViews
,它将会调用 nextView
numberOfActiveView
次,并将它们插入到后端。在每次滑动视图时也会调用 loadViews
。
public func loadViews()
// Usage:
swipeableView.numberOfActiveView = 3
swipeableView.nextView = {
return UIView()
}
swipeableView.loadViews() // optional, automatically call after nextView is set
将视图添加到前端
previousView
与 rewind
一起使用,它将在前端插入一个视图,并通过动画将其定位到中心位置。注意,当 history
为空时,rewind
将调用 previousView
,否则它将返回最后滑动的视图。请尝试浏览 Previous View 示例以获取更多信息。
public func rewind()
// Usage:
swipeableView.previousView = {
return UIView()
}
swipeableView.rewind()
界面构建器
如果您需要使用界面构建器,演示应用程序包含创建视图的程序示例以及从 Xib 文件加载视图的示例,这些 Xib 文件使用了自动布局,并可以参考 自动布局的说明。
删除视图
通过编程方式滑动
用户可以按照允许的方向在视图中滑动。这也可以通过编程实现。
您可以通过预定义的方向或使用视图坐标中的点和方向来编程性地在顶部视图中滑动。
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))
撤销操作
ZLSwipeableView
会保留滑动视图的历史记录。可以通过调用rewind
来检索这些历史记录。
public var history = [UIView]()
public var numberOfHistoryItem = UInt(10)
public func rewind()
删除所有视图
要丢弃所有视图并编程性地重新加载(丢弃的视图不能通过撤销操作恢复)
swipeableView.discardViews()
swipeableView.loadViews()
授权
以下是可以监听的回调列表
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")
}
自定义
以下是可自定义的行为列表
public var animateView = ZLSwipeableView.defaultAnimateViewHandler()
public var interpretDirection = ZLSwipeableView.defaultInterpretDirectionHandler()
public var shouldSwipeView = ZLSwipeableView.defaultShouldSwipeViewHandler()
public var minTranslationInPercent = CGFloat(0.25)
public var minVelocityInPointPerSecond = CGFloat(750)
public var allowedDirection = Direction.Horizontal
interpretDirection
您可以通过重写interpretDirection
属性来改变方向解析的方式。有关详细信息,请参阅自定义滑动示例。
animateView
您可以通过重写 animateView
属性来创建自定义动画。请查看自定义动画示例以获取详细信息。
Should Swipe
参数 shouldSwipeView
、minTranslationInPercent
和 minVelocityInPointPerSecond
决定了是否可以滑动。请参考Should Swipe示例以了解详细信息。
allowedDirection
allowedDirection
属性限制了用户允许滑动方向。请查看自定义方向示例以获取详细信息。
swipeableView.allowedDirection = .Left | .Up
swipeableView.allowedDirection = .All
Misc
public func topView() -> UIView?
public func activeViews() -> [UIView]
Requirements
- iOS 7 或更高版本。
Credits
许可
ZLSwipeableViewSwift遵从MIT许可协议。更多详情请参阅LICENSE文件。