JSReorderableCollectionView
支持单元格拖拽交互的集合视图。
快照
要求
- Xcode 10.2.1
- Swift 5.0
- iOS 9.0+
但是这是我的构建设置。我认为除了一些特定情况外,应该没问题。
安装
CocoaPods
pod 'JSReorderableCollectionView', '~> 1.0.5'
抱歉,目前不支持 Carthage
。
用法
您有一个继承自 UICollectionView
的类和 1 个 JSReorderableCollectionView
协议。
// JSReorderableCollectionView
open class JSReorderableCollectionView: UICollectionView {
var isAxisFixed: Bool
var scrollThreshold: CGFloat
var scrollInset: UIEdgeInsets
var canMoveSection: Bool
public func beginInteractiveWithLocation(_ location: CGPoint)
public func updateInteractiveWithLocation(_ location: CGPoint)
public func finishInteractive()
}
// JSReorderableCollectionViewDelegate
public protocol JSReorderableCollectionViewDelegate: class {
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, willSnapshot cell: UICollectionViewCell, at point: CGPoint) -> UIView
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, willAppear snapshot: UIView, source cell: UICollectionViewCell, at point: CGPoint)
}
-
JSReorderableCollectionView
perpertes
isAxisFixed
(默认false
)
如果这个属性设置为
true
,选中点和快照位置将根据滚动方向固定。例如 -> 水平滚动收藏视图 ->
cell
.frame
.center
.y
=collectionView
.center
.y
scrollThreshold
(默认40
)
根据滚动方向,从边缘自动滚动区域的大小。
scrollInset
(默认1
)
拖动交互点不能超出收藏视图的范围。
scrollInset
作为找到该点的indexPath
的内边距。如果你的收藏视图有非 0 的
contentInset
,你应该设置此属性以找到单元格的indexPath
。canMoveSection
(默认false
)
如果此属性为
true
,收藏视图项可以移动到其他部分。你必须实现数据源中的
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
以首先更新数据模型。如果不这样做,部分移动可能不会正常工作。方法
func beginInteractiveWithLocation(_ location: CGPoint)
开始拖拽交互。你应该传递收藏视图的父视图的
CGPoint
。func updateInteractiveWithLocation(_ location: CGPoint)
更新单元格位置。
func finishInteractive()
完成拖拽 & 放置交互。
-
JSReorderableCollectionViewDelegate
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
如果你的收藏视图有超过两种单元格类型,你不想移动特定的单元格类型,则重写此方法。
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, willSnapshot cell: UICollectionViewCell, at point: CGPoint) -> UIView
如果你想自定义单元格快照视图,则重写此方法。(默认是单元格的原始快照)
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, willDisplay snapshot: UIView, source cell: UICollectionViewCell, at point: CGPoint)
如果你想自定义单元格快照显示效果或动画,则重写此方法。(默认是 1.1 倍大小 & 90% 不透明度)
如何知道单元格已移动
你可以使用 func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
of UICollectionViewDataSource
。
在 func updateInteractiveWithLocation(_ location: CGPoint)
中当 moveItem
被调用时发送事件。
已知问题
- 当选中的单元格由于自动滚动而消失时,将使单元格顺序看起来很奇怪,但数据集是正确的。
这仅在跨由多行(列或行)组成的集合视图中的单元格空间移动时发生。
贡献
欢迎任何想法,解决已知问题(请解决这个问题 T.T),新的问题,PR 都很受欢迎。
许可证
JSReorderableCollectionView 适用于 MIT 许可证。