SwiftReorder
SwiftReorder 是一个让您能够添加长按拖放排序的 UITableViewCell 扩展。它稳健、轻量且完全可自定义。
功能
- 平滑动画
- 自动边缘滚动
- 与多个表格节区合作
- 可自定义阴影、缩放和透明度效果
安装
CocoaPods
要使用 CocoaPods 将 SwiftReorder 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
pod 'SwiftReorder', '~> 7.2'
Carthage
要使用Carthage将SwiftReorder集成到您的Xcode项目中,请在您的Cartfile
中指定它。
github "adamshin/SwiftReorder" ~> 7.2
请记住要将SwiftReorder添加到您的Carthage构建阶段【1】。
$(SRCROOT)/Carthage/Build/iOS/SwiftReorder.framework
并且
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/SwiftReorder.framework
手动
您可以通过将Source
文件夹的内容复制到您的项目中,手动将SwiftReorder集成到项目中。
用法
设置
- 将以下行添加到您的表格视图设置中。
override func viewDidLoad() {
// ...
tableView.reorder.delegate = self
}
- 将此代码添加到您的
tableView(_:cellForRowAt:)
的开始部分。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let spacer = tableView.reorder.spacerCell(for: indexPath) {
return spacer
}
// ...
}
- 实现
tableView(_:reorderRowAt:to:)
代理方法,以及其他必要的方法。
extension MyViewController: TableViewReorderDelegate {
func tableView(_ tableView: UITableView, reorderRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Update data model
}
}
此方法类似于UITableViewDataSource
方法tableView(_:moveRowAt:to:)
。然而,它可能在一次拖放操作的过程中被多次调用。
定制
SwiftReorder暴露了几个属性来调整重排效果的样式。例如,您可以对选中单元格添加缩放效果。
tableView.reorder.cellScale = 1.05
或者调整阴影
tableView.reorder.shadowOpacity = 0.5
tableView.reorder.shadowRadius = 20