受流行的 Mailbox App 启发的可滑动 UITableViewCell,使用 Swift 实现。
.exit
模式是原始行为,类似于 Mailbox app。
.toggle
是另一种行为,单元格在滑动后会弹回。
Swift Package Manger 是 Apple 官方用于管理源代码分发的工具,旨在方便地共享您的代码和重用他人的代码。
要在 SwiftPM 项目中使用 SwipyCell
库,请将以下行添加到您的 Package.swift
文件中的依赖项中
.package(url: "https://github.com/moritzsternemann/SwipyCell", .upToNextMinor(from: "4.1.0")),
CocoaPods 是 Cocoa 项目的依赖项管理器。
要使用 CocoaPods 将 SwipyCell 集成到您的项目中,请将其添加到您的 Podfile
pod 'SwipyCell', '~> 4.1'
Carthage 是一个去中心化的依赖项管理器,它可以自动执行将框架添加到您的 Cocoa 应用程序的过程。
要使用 Carthage 将 SwipyCell 集成到您的项目中,请将其添加到您的 Cartfile
github "moritzsternemann/SwipyCell" >= 4.1
完整的示例在 Example 目录中可用。以下代码是一个非常基本的示例
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 表示滑动状态,例如单元格左侧的第一个状态。
可能的值有
.none
- 单元格的中心位置.state(index, side)
- 状态的索引(从近到远)和状态的侧面,均相对于单元格如上所示的 SwipyCellMode。
SwipyCellTriggerBlock 是以下类型的别名
(SwipyCell, SwipyCellTrigger, SwipyCellState, SwipyCellMode) -> Void
使用此方法可以轻松地向单元格添加滑动触发器
func addSwipeTrigger(forState: SwipyCellState, withMode: SwipyCellMode, swipeView: UIView, swipeColor: UIColor, completion: SwipyCellTriggerBlock)
forState
触发器应激活的状态withMode
swipeView
:例如,显示一个图标swipeColor
:swipeView 的 backgroundColorcompletion
:在滑动操作结束后调用,仅当达到触发点时调用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%
var swipeViewPadding: CGFloat
swipeViewPadding 是滑动视图与单元格外边缘之间的填充。
默认值:24.0
var shouldAnimateSwipeViews: Bool
shouldAnimateSwipeViews
设置滑动视图是否应在滑动时随单元格移动,还是停留在外边缘。
默认值:true
var defaultSwipeViewColor: UIColor
defaultSwipeViewColor
是当前状态为 .none
时滑动的颜色。
默认值:UIColor.white
当使用 .exit
模式时,您可以使用 swipeToOrigin(_:)
方法将单元格动画返回到其默认位置。如果您的应用程序要求用户确认,而用户想要取消操作,这将非常有用。
cell.swipeToOrigin {
print("Swiped back")
}
我是 Moritz Sternemann,慕尼黑 工业大学 的计算机科学学生。
SwipyCell 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。