The collection view which can drag & drop interaction of cell.
- Xcode 15.0+
- Swift 5.9+
- iOS 12.0+
But it is my build setting. I think it will be fine except specific cases.
dependencies: [ .package(url: "https://github.com/wlsdms0122/JSReorderableCollectionView.git", .upToNextMajor(from: "1.6.0")) ]
You have 1 class inherited UICollectionView
and 1 protocol for JSReorderableCollectionView
.
// JSReorderableCollectionView
open class JSReorderableCollectionView: UICollectionView {
/// Fix snapshot point depended on scroll direction
public var isAxisFixed: Bool
/// Threshold of auto scrolling. must be greater than zero.
public var scrollThreshold: CGFloat
/// Speed of auto scrolling. must be grater than zero.
public var scrollSpeed: CGFloat
/// Point boundary inset
public var scrollInset: UIEdgeInsets
/// Item can move other section
public 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)
func reorderableCollectionView(_ collectionView: JSReorderableCollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
}
isAxisFixed
(default false
)
If this propery ture
, select point and snapshot position will fix depend on scroll direction.
scrollThreshold
(default 40
)
Auto scroll area size from both edges depend on scroll direction. It must be greater than zero.
scrollSpeed
(default 10
)
Auto scroll speed. It must be greater than zero.
scrollInset
(default 1
)
Drag interaction point can't out of collection view's bounds. scrollInset
is inset for find indexPath
of cell on the point.
If your collection view have contentInset
not 0, you should set this property for find indexPath
of cell.
canMoveSection
(default false
)
If this property true
, collection view item can move to other section.
func beginInteractiveWithLocation(_ location: CGPoint)
Start drag interaction. you should pass CGPoint
of super view of collection view
func updateInteractiveWithLocation(_ location: CGPoint)
Update cell position.
func finishInteractive()
Finish drag & drop interaction.
func reorderableCollectionView(_:canMoveItemAt:indexPath:) -> Bool
If your collection view have over two kind of cell type and you won't to move specific cell type, override this method.
func reorderableCollectionView(_:willSnapshot:at:) -> UIView
If you want to customize cell snapshot view, override this method. (default is origin snapshot of the cell)
func reorderableCollectionView(_:willDisplay:source:at:)
If you want to customize cell snapshot display effect or animation, override this method. (default is 1.1x size & 90% alpha)
func reorderableCollectionView(_:moveItemAt:to:)
If you use something like a UICollectionViewDiffableDataSource
, default implementation occur fatal error. In this case, you can implement own move sequence.
You must implement func collectionView(_:moveItemAt:to:)
of datasource to update data model first. If not section move isn't working well.
- When the selectd cell is disappeared by auto scroll, it makes cell order look strange. but data set is fine.
It is only occur that moving between cell spaces in the collection view consist of more than one line (column or row).
Any contributions are welcome.
JSReorderableCollectionView
is available under the MIT license.