SwipyCell
受热门Mailbox App启发的可滑动 UITableViewCell,使用 Swift 编写。
预览
退出模式
.exit 模式是原始行为,来自 Mailbox 应用程序。
切换模式
.toggle
是另一种行为,在该行为中,单元格在被滑动后会弹回。
安装
Swift Package Manager(推荐)
Swift Package Manager 是苹果的第一方工具,用于管理源代码的发布,旨在简化源代码的共享和重用。
要在 SwiftPM 项目中使用 SwipyCell
库,请将以下行添加到您的 Package.swift
文件中的依赖关系。
.package(url: "https://github.com/moritzsternemann/SwipyCell", .upToNextMinor(from: "4.1.0")),
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。
要使用 CocoaPods 将 SwipyCell 集成到您的项目中,请将其添加到您的 Podfile
pod 'SwipyCell', '~> 4.1'
Carthage
Carthage 是一个去中心化的依赖管理器,可以自动将框架添加到您的 Cocoa 应用程序中。
要使用 Carthage 将 SwipyCell 集成到您的项目中,请将其添加到您的 Cartfile
github "moritzsternemann/SwipyCell" >= 4.1
用法
示例
一个完整的示例可以在 示例 目录中找到。以下代码是一个非常基本的示例。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SwipyCell
cell.selectionStyle = .gray
cell.contentView.backgroundColor = UIColor.white
let checkView = viewWithImageName("check")
let greenColor = UIColor(red: 85.0 / 255.0, green: 213.0 / 255.0, blue: 80.0 / 255.0, alpha: 1.0)
let crossView = viewWithImageName("cross")
let redColor = UIColor(red: 232.0 / 255.0, green: 61.0 / 255.0, blue: 14.0 / 255.0, alpha: 1.0)
let clockView = viewWithImageName("clock")
let yellowColor = UIColor(red: 254.0 / 255.0, green: 217.0 / 255.0, blue: 56.0 / 255.0, alpha: 1.0)
let listView = viewWithImageName("list")
let brownColor = UIColor(red: 206.0 / 255.0, green: 149.0 / 255.0, blue: 98.0 / 255.0, alpha: 1.0)
cell.defaultColor = tableView.backgroundView?.backgroundColor
cell.delegate = self
cell.textLabel?.text = "Switch Mode Cell"
cell.detailTextLabel?.text = "Swipe to switch"
cell.addSwipeTrigger(forState: .state(0, .left), withMode: .toggle, swipeView: checkView, swipeColor: greenColor, completion: { cell, trigger, state, mode in
print("Did swipe \"Checkmark\" cell")
})
cell.addSwipeTrigger(forState: .state(1, .left), withMode: .toggle, swipeView: crossView, swipeColor: redColor, completion: { cell, trigger, state, mode in
print("Did swipe \"Cross\" cell")
})
cell.addSwipeTrigger(forState: .state(0, .right), withMode: .toggle, swipeView: clockView, swipeColor: yellowColor, completion: { cell, trigger, state, mode in
print("Did swipe \"Clock\" cell")
})
cell.addSwipeTrigger(forState: .state(1, .right), withMode: .toggle, swipeView: listView, swipeColor: brownColor, completion: { cell, trigger, state, mode in
print("Did swipe \"List\" cell")
})
return cell
}
SwipyCellState
SwipyCellState 表示滑动状态,例如单元格左侧的第一个状态。
可能的值有
.none
- 单元格的中心位置.state(index, side)
- 从近到远的状态 index 和状态 side ,每个都是相对于单元格的
SwipyCellMode
如上所示,SwipyCellMode。
SwipyCellTriggerBlock
SwipyCellTriggerBlock 是对
(SwipyCell, SwipyCellTrigger, SwipyCellState, SwipyCellMode) -> Void
向单元格添加滑动触发器
使用此方法向单元格添加滑动触发器很简单
func addSwipeTrigger(forState: SwipyCellState, withMode: SwipyCellMode, swipeView: UIView, swipeColor: UIColor, completion: SwipyCellTriggerBlock)
forState
在触发器应激活的状态withMode
用于触发器swipeView
:例如显示一个图标swipeColor
:swipeView 的背景颜色completion
:在滑动手势结束后调用,仅当触发点被到达时
Delegate
SwipyCell 提供了三个代理方法来跟踪用户行为。
// When the user starts swiping the cell this method is called
func swipyCellDidStartSwiping(_ cell: SwipyCell)
// When the user ends swiping the cell this method is called
func swipyCellDidFinishSwiping(_ cell: SwipyCell, atState state: SwipyCellState, triggerActivated activated: Bool)
// When the user is dragging, this method is called with the percentage from the border
func swipyCell(_ cell: SwipyCell, didSwipeWithPercentage percentage: CGFloat, currentState state: SwipyCellState, triggerActivated activated: Bool)
配置
所有可配置的选项都在单例对象 SwipyCellConfig.shared
中定义。每个新的单元格都将这些选项设置为默认值。要更改默认值,只需更改 SwipyCellConfig
单例对象的变量。
触发点
触发点在配置单例中的 triggerPoints<CGFloat, SwipyCellState>
字典或每个单元格中单独定义。
每个键标记触发点的滑动百分比;相应的值是用于稍后引用触发点的标识符。负键标记单元格右侧的点(向左滑动),正键标记单元格左侧的点(向右滑动)。
要修改触发点,每个单元格以及配置单例都提供了一些方法。
// Set a new trigger point for the given state
func setTriggerPoint(forState state: SwipyCellState, at point: CGFloat)
// Set a new trigger point for the given index on BOTH sides of the cell
func setTriggerPoint(forIndex index: Int, at point: CGFloat)
// Overwrite all existing trigger points with the given new ones
func setTriggerPoints(_ points: [CGFloat: SwipyCellState])
// The Integer parameter is the index for BOTH sides of the cell
func setTriggerPoints(_ points: [CGFloat: Int])
// Overwrite all existing trigger points with new ones in order of the array on BOTH sides
func setTriggerPoints(points: [CGFloat])
// Get all existing trigger points
func getTriggerPoints() -> [CGFloat: SwipyCellState]
// Clear all existing trigger points
func clearTriggerPoints()
默认值:每侧25%和75%
swipeViewPadding
var swipeViewPadding: CGFloat
swipeViewPadding 是滑动视图和单元格外边缘之间的填充。
默认值: 24.0
shouldAnimateSwipeViews
var shouldAnimateSwipeViews: Bool
shouldAnimateSwipeViews
设置是否在滑动时让滑动视图与单元格一起移动或保持在外边缘。
默认值: true
defaultSwipeViewColor
var defaultSwipeViewColor: UIColor
defaultSwipeViewColor
当当前状态为 none
时,是滑动时的颜色。
默认值: UIColor.white
重置单元格位置
当使用 .exit
模式时,您可以使用 swipeToOrigin(_:)
方法将单元格动画回到其默认位置。如果您希望用户确认并取消操作,这可能很有用。
cell.swipeToOrigin {
print("Swiped back")
}
作者
我是 Moritz Sternemann,慕尼黑工业大学的一名计算机科学学生。
- 电子邮件: [email protected]
- 推特: @strnmn
- 领英: /in/moritzsternemann
许可证
SwipyCell 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。