Reorder
介绍
要求
Reorder
使用 Swift 5.0 编写。兼容 iOS 8.0+
安装
CocoaPods
Reorder 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的 Podfile 中
pod 'Reorder'
使用方法
UITableView
class TableViewController: UIViewController {
let tableView = UITableView(frame: .zero, style: .plain)
var list = [[Int]]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reorder.delegate = self
}
}
// MARK: TableViewReorderDelegate
extension TableViewController: TableViewReorderDelegate {
var reorderSuperview: UIView {
return self.navigationController?.view ?? UIView()
}
func tableViewReorder(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath.section == destinationIndexPath.section { // Row
self.list[sourceIndexPath.section].swapAt(sourceIndexPath.row, destinationIndexPath.row)
} else { // MultipleSection
let sourceItem = self.list[sourceIndexPath.section][sourceIndexPath.row]
self.list[sourceIndexPath.section].remove(at: sourceIndexPath.row)
self.list[destinationIndexPath.section].insert(sourceItem, at: destinationIndexPath.row)
}
}
func tableViewReorder(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return self.list[indexPath.section][indexPath.row].canMove
}
}
UICollectionView
class CollectionViewController: UIViewController {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: Layout())
var list = [[Int]]()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.reorder.delegate = self
}
}
// MARK: CollectionViewReorderDelegate
extension MultipleSectionCollectionViewExampleViewController: CollectionViewReorderDelegate {
var reorderSuperview: UIView {
return self.navigationController?.view ?? UIView()
}
func collectionViewReorder(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if sourceIndexPath.section == destinationIndexPath.section { // Item
let item = self.list[sourceIndexPath.section][sourceIndexPath.item]
self.list[sourceIndexPath.section].remove(at: sourceIndexPath.item)
self.list[sourceIndexPath.section].insert(item, at: destinationIndexPath.item)
} else { // MultipleSection
let sourceItem = self.list[sourceIndexPath.section][sourceIndexPath.row]
self.list[sourceIndexPath.section].remove(at: sourceIndexPath.row)
self.list[destinationIndexPath.section].insert(sourceItem, at: destinationIndexPath.row)
}
}
}
滑动手动滚动
self.tableView.reorder.scrollFrame = CGRect(x: 0, y: 88, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 88)
self.collectionView.reorder.scrollFrame = CGRect(x: 0, y: 88, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 88)
无法移动
// MARK: TableViewReorderDelegate
extension ExampleViewController: TableViewReorderDelegate {
func tableViewReorder(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return self.list[indexPath.row].canMove
}
}
属性
缩放
在单元格触摸时查看快照尺寸
enum Scale {
case custom(CGFloat)
case none
case small
case medium
case large
var scale: CGFloat {
switch self {
case .none: return 1
case .small: return 1.02
case .medium: return 1.05
case .large: return 1.08
case .custom(let scale): return scale
}
}
}
OverlapBehaviour
在移动单元格时与其他单元格重叠的数量
public enum OverlapBehaviour {
case oneThird
case half
case twoThirds
// UP & LEFT
func minus(_ value: CGFloat) -> CGFloat {
switch self {
case .oneThird: return value / 3 * 1
case .half: return value / 2
case .twoThirds: return value / 3 * 2
}
}
// DOWN & BOTTOM
func plus(_ value: CGFloat) -> CGFloat {
switch self {
case .oneThird: return value / 3 * 2
case .half: return value / 2
case .twoThirds: return value / 3 * 1
}
}
}
Delegate
protocol ReorderDelegate: class {
var reorderSuperview: UIView { get }
func reorderBegan()
func reorderChanged()
func reorderEnded()
}
protocol CollectionViewReorderDelegate: ReorderDelegate {
func collectionViewReorder(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
func collectionViewReorder(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
}
protocol TableViewReorderDelegate: ReorderDelegate {
func tableViewReorder(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
func tableViewReorder(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
}
Author
pikachu987, [email protected]
License
重排根据MIT许可协议提供。更多信息,请查阅LICENSE文件。