SwipeCellKit
基于内置 Mail.app 实现的,基于 Swift 的可滑动 UITableViewCell/UICollectionViewCell。
关于
支持左右滑动操作的可滑动 UITableViewCell
或 UICollectionViewCell
,具有以下功能:
- 左右滑动操作
- 动作按钮,支持:仅文本、文本 + 图片、仅图片
- 触觉反馈
- 可自定义过渡效果:边框、拖动和显示
- 在滑动期间可自定义动作按钮行为
- 超过阈值时进行动画扩展
- 可自定义扩展动画
- 支持
UITableView
和UICollectionView
- 无障碍功能
- 深色模式
背景
阅读我关于 SwipeCellKit 从何而来的博客文章:Swipe No Swiping。
示例
转换样式
转换样式描述了在滑动过程中动作按钮的暴露方式。
边框
拖拽
展现
自定义
展开样式
展开样式描述了当单元格滑动超过定义阈值时的行为。
无
选择
破坏性
定制化
需求
- Swift 5.0
- Xcode 10.3+
- iOS 9.0+
安装
CocoaPods (推荐)
use_frameworks!
# Latest release in CocoaPods
pod 'SwipeCellKit'
# Get the latest on develop
pod 'SwipeCellKit', :git => 'https://github.com/SwipeCellKit/SwipeCellKit.git', :branch => 'develop'
# If you have NOT upgraded to Xcode 11, use the last Swift Xcode 10.X compatible release
pod 'SwipeCellKit', '2.6.0'
# If you have NOT upgraded to Swift 5.0, use the last Swift 4.2/Xcode 10.2 compatible release
pod 'SwipeCellKit', '2.5.4'
# If you have NOT upgraded to Swift 4.2, use the last non-swift 4.2 compatible release
pod 'SwipeCellKit', '2.4.3'
Carthage
github "SwipeCellKit/SwipeCellKit"
文档
阅读文档。由jazzy生成。托管于GitHub Pages。
UITableView使用方法
在SwipeTableViewCell
上设置delegate
属性。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! SwipeTableViewCell
cell.delegate = self
return cell
}
采用SwipeTableViewCellDelegate
协议。
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
// handle action by updating model with deletion
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete")
return [deleteAction]
}
可选地,您可以实现editActionsOptionsForRowAt
方法来自定义滑动动作的行为。
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeOptions()
options.expansionStyle = .destructive
options.transitionStyle = .border
return options
}
UICollectionView使用方法
在SwipeCollectionViewCell
上设置delegate
属性。
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! SwipeCollectionViewCell
cell.delegate = self
return cell
}
采用SwipeCollectionViewCellDelegate
协议。
func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
// handle action by updating model with deletion
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete")
return [deleteAction]
}
可选地,您可以实现editActionsOptionsForItemAt
方法来自定义滑动动作的行为。
func collectionView(_ collectionView: UICollectionView, editActionsOptionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeOptions()
options.expansionStyle = .destructive
options.transitionStyle = .border
return options
}
过渡效果
SwipeTransitionStyle
提供了三种内置的过渡风格。
- .border:可操作的显示区域被所有操作按钮平均分割。
- .drag:可操作显示区域被拖动并固定在单元格上,每个操作按钮在曝光时具有满尺寸。
- .reveal:可操作显示区域位于单元格后,固定在表视图边缘,并在单元格被拖动到一边时被揭示。
有关自定义按钮外观的详细信息,请参阅自定义过渡效果。
过渡代理
可以通过将SwipeActionTransitioning
设置到transitionDelegate
属性上来观察SwipeAction
的过渡。这允许您观察可见百分比并访问那个SwipeAction
的底层UIButton
。
扩展
由 SwipeExpansionStyle
提供了四种内置扩展样式
- .selection
- .destructive(类似于 Mail.app)
- .destructiveAfterFill(类似于 Mailbox/Tweetbot)
- .fill
为了实现高程度的可定制性,我们对 SwipeExpansionStyle
做了大量工作。如果这些内置样式无法满足您的需求,请参阅自定义扩展指南以获取更多关于创建自定义样式的详细信息。
内置的 .fill
扩展样式需要手动完成操作。这意味着您的动作处理程序必须在调用过程中或之后某个时刻调用 SwipeAction.fulfill(style:)
来解决填充扩展。提供的 ExpansionFulfillmentStyle
允许您在稍后的某个时刻删除或重置单元格(可能是在进一步的用户交互之后)。
内置的 .destructive
和 .destructiveAfterFill
扩展样式在动作处理程序被调用时配置为自动执行行删除(自动完成)。您的删除行为可能需要与其他行动画协调(例如在 beginUpdates
和 endUpdates
内)。在这种情况下,您可以轻松创建一个需要手动完成的自定义 SwipeExpansionStyle
以触发删除。
var options = SwipeTableOptions()
options.expansionStyle = .destructive(automaticallyDelete: false)
**注意**:您必须在动作处理程序调用过程中/之后某个时刻调用
SwipeAction.fulfill(with style:)
以触发删除。请勿直接调用deleteRows
。
let delete = SwipeAction(style: .destructive, title: nil) { action, indexPath in
// Update model
self.emails.remove(at: indexPath.row)
action.fulfill(with: .delete)
}
高级
请参阅高级指南了解更多关于定制的详细信息。
致谢
由 @mkurabi 维护。
展示
我们很想知道谁在使用 SwipeCellKit 的应用程序。请提交一个拉取请求以添加您自己的应用程序!
许可协议
SwipeCellKit
依据 MIT 许可协议 发布。请参阅 LICENSE
文件以获取详细信息。
请提供适当的署名,这对我们至关重要。